【问题标题】:Custom ArrayAdapter showing a blank screen in my Fragment自定义 ArrayAdapter 在我的片段中显示空白屏幕
【发布时间】:2014-08-19 06:24:57
【问题描述】:

所以我刚刚在 android 上玩了一会儿,我遇到了一些障碍。由于某种原因,我实例化 ListOfJokesTypesAdapter 的片段没有显示填充了我的 JokeData 类中的数据的列表视图。

我得到的只是一个空白屏幕(没有错误或任何类似性质的东西)。

这只是我一直在进行的概念验证,因此我们将不胜感激。为什么我的 MainClassAdapter 工作正常时这个特定的自定义适配器不工作。

代码

我的笑话课:

public class Joke {

private String jokeSetup;
private String jokePunchline;

public Joke(String jokeSetup, String jokePunchline) {
    this.jokeSetup = jokeSetup;
    this.jokePunchline = jokePunchline;
}

public String getJokeSetup() {
    return jokeSetup;
}

public void setJokeSetup(String jokeSetup) {
    this.jokeSetup = jokeSetup;
}

public String getJokePunchline() {
    return jokePunchline;
}

public void setJokePunchline(String jokePunchline) {
    this.jokePunchline = jokePunchline;
 }
}

我的笑话列表类

public class JokeListData {

private String listName;
private List<Joke> arrayListOfJokes;

public JokeListData(String listName, List<Joke> arrayListOfJokes) {
    this.listName = listName;
    this.arrayListOfJokes = arrayListOfJokes;
}

public String getListName() {
    return listName;
}

public void setListName(String listName) {
    this.listName = listName;
}

public List<Joke> getArrayListOfJokes() {
    return arrayListOfJokes;
}

public void setArrayListOfJokes(ArrayList<Joke> arrayListOfJokes) {
    this.arrayListOfJokes = arrayListOfJokes;
}
}

我的真实笑话数据

public class JokeData {



private static List<Joke> dogJokes = new ArrayList<Joke>(){

    {
        add(new Joke("Dogs", "Bark"));
        add(new Joke("Dogs", "Woof"));
        add(new Joke("Dogs", "Howl"));
        add(new Joke("Dogs", "Sniff"));
    }
};


private static List<Joke> catJokes = new ArrayList<Joke> (){
    {
        add(new Joke("Cats", "woof"));
        add(new Joke("Dogs", "Meow"));
    }
};

static List<JokeListData> dataOfJokeList = new ArrayList<JokeListData>();

public static void addEntries(){
    dataOfJokeList.add(new JokeListData("Cat Jokes", catJokes));
    dataOfJokeList.add(new JokeListData("Dog Jokes", dogJokes));
 }

}

适配器

public class ListOfJokeTypesAdapter extends ArrayAdapter<JokeListData> {

Context mContext;
int mLayoutId;
List<JokeListData> mList;

public ListOfJokeTypesAdapter(Context context, int resource, List<JokeListData> objects) {
    super(context, resource, objects);
    this.mContext = context;
    this.mLayoutId = resource;
    this.mList = objects;
}


@Override
public int getCount() {
    return super.getCount();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;

    if(convertView == null){
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(mLayoutId,parent,false);

        holder = new ViewHolder();

        holder.mTextView = (TextView) convertView.findViewById(R.id.rowForMainList);

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    JokeListData jokeListData = mList.get(position);

    holder.mTextView.setText(jokeListData.getListName());

    return convertView;
}

private static class ViewHolder{
    TextView mTextView;
 }
}

使用适配器的片段

public class ListOfJokeTypesFragment extends Fragment {

ListView mListView;
ListOfJokeTypesAdapter listOfJokeTypesAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.joke_type_fragment,container,false);
    JokeData.addEntries();
    mListView = (ListView)view.findViewById(R.id.jokeTypeListView);
    listOfJokeTypesAdapter = new ListOfJokeTypesAdapter(getActivity().getApplicationContext(),R.layout.row,JokeData.dataOfJokeList);
    mListView.setAdapter(listOfJokeTypesAdapter);

    return view;
 }
}

片段管理器 包 com.example.taranveer.jokeapplicationactual;

导入android.app.Activity; 导入android.os.Bundle;

/** * 由 Taranveer 于 2014-07-22 创建。 */ 公共类 TheFragmentActivityManager 扩展 Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_container);

    Bundle args = getIntent().getExtras();

    if(findViewById(R.id.container) != null){

           if(args != null){
               if(args.getInt("randomjoke") == 1){
                  RandomJokeFragment randomJokeFragment = new RandomJokeFragment();
                  getFragmentManager().beginTransaction()
                                      .replace(R.id.container, randomJokeFragment)
                                       .commit();
               }
           }
    }
    if(findViewById(R.id.container) != null){

        if(args!=null){
            if(args.getInt("listofjoketypes") == 2){
                ListOfJokeTypesFragment listOfJokeTypesFragment = new ListOfJokeTypesFragment();
                getFragmentManager().beginTransaction()
                                    .replace(R.id.container,listOfJokeTypesFragment)
                                    .commit();
            }
        }
    }

}
}

相关的 XML:

joke_type_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<ListView
    android:id="@+id/jokeTypeListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</ListView>

</LinearLayout>

row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_margin="5dp"
android:gravity="center_vertical"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
    android:textColor="#eeeeee"
    android:textStyle="bold"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:background="#2299dd"
    android:textSize="20sp"
    android:text="Main Activity Items"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rowForMainList"/>
</LinearLayout>

</LinearLayout>

【问题讨论】:

  • @Override public int getCount() { return mList.getCount(); }

标签: java android xml android-fragments


【解决方案1】:

最好把这些行 JokeListData jokeListData = mList.get(position);

holder.mTextView.setText(jokeListData.getListName()); 

if(convertView == null) 条件内

【讨论】:

  • 我试过这个,但没有用。我添加了一个 Log.e 函数,我的 JokeData 类中的数据似乎没有进入。有什么想法吗?
  • 在您的 onCreate() 中有多个 if 条件,因此对于测试,请尝试删除所有条件并仅测试添加: ListOfJokeTypesFragment listOfJokeTypesFragment = new ListOfJokeTypesFragment(); getFragmentManager().beginTransaction() .replace(R.id.container,listOfJokeTypesFragment).commit();它应该显示还检查列表的 texcolor 和背景颜色是否相同尝试使列表的列表背景颜色和文本颜色不同。
【解决方案2】:
beforr@Override
public int getCount() {
    return super.getCount();
}

删除这一行

【讨论】:

  • 删除此行不会显示任何内容。适配器需要知道它需要显示多少数据。特别是当它基于列表显示时
【解决方案3】:

您需要在自定义适配器中覆盖 getCount()。 它将返回 ListView 中的条目数。 替换

@Override
public int getCount() {
    return super.getCount();
}

类似的东西

@Override
public int getCount() {
    return mySourceArrayList.getCount();
}

【讨论】:

    猜你喜欢
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多