未调用自定义适配器 getview [英] Custom adapter getview is not called

查看:28
本文介绍了未调用自定义适配器 getview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 ListFragment自定义适配器,但是适配器 getView() 根本没有被调用.

I have a Custom adapter with a ListFragment but the adapters getView() is not getting called at all.

这是适配器的样子 -

This is how the adapter looks like -

public class ModuleListItemAdapter extends BaseAdapter {

    List<ModuleItem> list;
    Context context;
    Module mod;

    public ModuleListItemAdapter() {
        super();
        // TODO Auto-generated constructor stub
    }

    public ModuleListItemAdapter(Context context, List<ModuleItem> list, Module mod) {
        super();
        this.list = list;
        this.context = context;
        this.mod = mod;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.moduleitem, null);
        GenerateModuleItemView gmiv = new GenerateModuleItemView(context);
        rl.addView(gmiv.itemDispView(mod.getFields(), list.get(position)));
        return rl;
    }

    public void setValue(List<ModuleItem> l) {
        this.list = l;
        notifyDataSetChanged();
    }

    public void addValue(ModuleItem item) {
        this.list.add(item);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}

和片段 -

public class ModuleItemListFragment extends ListFragment {

    List<ModuleItem> list;
    Module mod;

    public ModuleItemListFragment() {
        super();
        // TODO Auto-generated constructor stub
    }

    public ModuleItemListFragment(List<ModuleItem> list, Module mod) {
        super();
        this.list = list;
        this.mod = mod;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.list, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ModuleListItemAdapter adapter = new ModuleListItemAdapter(getActivity(), list, mod);
        setListAdapter(adapter);
    }
}

这就是我的布局的样子 -

And this is how my layout looks like -

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@android:id/list"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:divider="#000000"
          android:dividerHeight="1dip" />

我不知道这里有什么问题.

I don't know what the problem is here.

推荐答案

这是因为 getCount() 返回零.您在 getCount() 中返回的数字是调用 getView() 的次数.在 getCount() 中,您应该始终返回列表的大小.

It's because getCount() returns zero. The number you return in getCount() is the times the getView() will be called. In getCount() you should always return the size of the list.

因此

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

@Override
public ModuleItem getItem(int position) {
    return list.get(position);
}

另外,也许布局的 ListView id 不是 android.R.id.list?

Also, maybe the layout's ListView id is not android.R.id.list?

确保你有 xml

<ListView
    android:id="@android:id/list"
    ...

另外,永远不要在构造函数中将任何数据传递给片段.

Also, don't ever pass any data to a fragment in constructor.

错误:

public ModuleItemListFragment(List<ModuleItem> list,Module mod) {
    super();
    this.list=list;
    this.mod=mod;
}

正确:

private static final String EXTRA_LIST = "ModuleItemListFragment.EXTRA_LIST";
private static final String EXTRA_MODULE = "ModuleItemListFragment.EXTRA_MODULE";

public static ModuleItemListFragment instantiate(ArrayList<ModuleItem> list, Module mod) {
    final Bundle args = new Bundle();
    args.putParcelable(EXTRA_LIST, list);
    args.putParcelable(EXTRA_MODULE, module);

    final ModuleItemListFragment f = new ModuleItemListFragment();
    f.setArguments(args);
    return f;
}

@Override
public void onCreate(Bundle state) {
    super.onCreate(state);
    final Bundle args = getArguments();
    this.list = args.getParcelable(EXTRA_LIST);
    this.module = args.getParcelable(EXTRA_MODULE);
}

当然,你必须让你的 Module Parcelable 或 Serializable.

Of course, you have to make your Module Parcelable or Serializable.

您必须指定 args,因为 Fragment 可以被系统终止和恢复,如果您通过 setter 或构造函数传递数据,它们将不会被恢复,因此在某些情况下可能变为 null.

You must specify args because the Fragment can be killed and restored by the system and if you pass data via setters or constructors they will not be restored and therefore can become null in some circumstances.

这篇关于未调用自定义适配器 getview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆