【问题标题】:How to fill ListView with the Custom Adapter? list view empty (如何用自定义适配器填充 ListView?列表视图为空(
【发布时间】:2013-04-07 03:27:35
【问题描述】:

我有用于列表视图的自定义适配器。程序正常运行,但列表视图为空。

我的适配器看起来像:

public class CustomAdapter extends BaseAdapter implements Filterable {

private ArrayList<OItem> _data;
Context _c;

public CustomAdapter(ArrayList<OItem> data, Context c) {
    _data = data;
    _c = c;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null)
    {
       LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       v = vi.inflate(R.layout.item, null);
    }

    OItem oItem = _data.get(position);

    TextView tvId = (TextView)v.findViewById(R.id.id);
    TextView tvName = (TextView)v.findViewById(R.id.name);
    TextView tvBatch = (TextView)v.findViewById(R.id.batch);

    tvId.setText(oItem.getId());
    tvName.setText(oItem.getName());
    tvBatch.setText(oItem.getBatch());

   return v;
}
}

活动中:

ArrayList<OItem> arrItems = new ArrayList<OItem>();
....
here I fill arrItens with the data
....
ListView lvSimple = (ListView) findViewById(R.id.lvContent);
lvSimple.setAdapter(new CustomAdapter(arrItems, this));

可能是什么问题? 也许应该在适配器的 getView 方法中添加一些东西?

谢谢

【问题讨论】:

  • 试试 _data = data.clone();
  • 您是否有特定的理由不使用 ArrayAdapter 而不是 BaseAdapter?

标签: android listview custom-adapter


【解决方案1】:

我假设您尚未实现 getCount,请将其添加到您的 CustomAdapter 中

   public int getCount() {
    return null == _data ? 0 : _data.size();
}

【讨论】:

  • 谢谢)我的 getCount 是空的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多