Android 如何使用自定义适配器填充ListView?列表视图为空(
我有自定义的listview适配器。程序运行时没有错误,但列表视图是空的 我的适配器看起来像:Android 如何使用自定义适配器填充ListView?列表视图为空(,android,listview,custom-adapter,Android,Listview,Custom Adapter,我有自定义的listview适配器。程序运行时没有错误,但列表视图是空的 我的适配器看起来像: public class CustomAdapter extends BaseAdapter implements Filterable { private ArrayList<OItem> _data; Context _c; public CustomAdapter(ArrayList<OItem> data, Context c) { _data = data
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;
}
}
公共类CustomAdapter扩展BaseAdapter实现可过滤{
私有ArrayList_数据;
语境;
公共CustomAdapter(ArrayList数据,上下文c){
_数据=数据;
_c=c;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null)
{
LayoutInflater vi=(LayoutInflater)\u c.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.布局.项目,空);
}
OItem OItem=_data.get(位置);
TextView tvId=(TextView)v.findViewById(R.id.id);
TextView tvName=(TextView)v.findViewById(R.id.name);
text视图tvBatch=(text视图)v.findviewbyd(R.id.batch);
tvId.setText(oItem.getId());
setText(oItem.getName());
tvBatch.setText(oItem.getBatch());
返回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));
ArrayList arrItems=new ArrayList();
....
这里我用数据填充arrItens
....
ListView lvSimple=(ListView)findViewById(R.id.lvContent);
setAdapter(新的CustomAdapter(arrItems,this));
有什么问题吗?
也许应该在适配器的getView方法中添加一些内容
谢谢我假设您尚未实现getCount,请将其添加到您的CustomAdapter中
public int getCount() {
return null == _data ? 0 : _data.size();
}
我假设您尚未实现getCount,请将其添加到CustomAdapter中
public int getCount() {
return null == _data ? 0 : _data.size();
}
试试_data=data.clone();你有没有不使用ArrayAdapter而不是BaseAdapter的具体原因?试试_data=data.clone();你有没有不使用ArrayAdapter而不是BaseAdapter的具体原因?谢谢)我的getCount为空谢谢)我的getCount为空