我有一个扩展了`SimpleCursorAdapter。
的类public class MyAdapter extends SimpleCursorAdapter {
private Cursor cursor;
private LayoutInflater inflater;
private int columnIndexRowID;
private int columnIndexFromAddrString;
private int columnIndexToAddrString;
public LastRequestsAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.cursor = c;
this.inflater = LayoutInflater.from(context);
this.columnIndexRowID = c.getColumnIndex(MyDBAdapter.KEY_ROWID);
this.columnIndexFromAddrString = c
.getColumnIndex(MyDBAdapter.KEY_FROM_ADDR_STRING);
this.columnIndexToAddrString = c
.getColumnIndex(MyDBAdapter.KEY_TO_ADDR_STRING);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null){
convertView = this.inflater.inflate(R.layout.main_activity_last_request_row, null);
viewHolder = new ViewHolder();
viewHolder.id = -1;
viewHolder.fromAddr = (TextView) convertView.findViewById(R.id.from);
viewHolder.toAddr =(TextView) convertView.findViewById(R.id.to);
viewHolder.arrow = (ImageView) convertView.findViewById(R.id.last_request_arrow);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
this.cursor.moveToPosition(position);
viewHolder.id = this.cursor.getInt(columnIndexRowID);
viewHolder.fromAddr.setText(this.cursor.getString(this.columnIndexFromAddrString));
viewHolder.toAddr.setText(this.cursor.getString(this.columnIndexToAddrString));
viewHolder.arrow.setImageResource(R.drawable.arrow_alt);
return convertView;
}
public class ViewHolder{
public int id;
public TextView fromAddr;
public TextView toAddr;
public ImageView arrow;
}
}
以这种方式使用:
listView.setAdapter(new MyAdapter(this,
R.layout.main_activity_last_request_row, dbCursor,
new String[] { MyDBAdapter.KEY_FROM_ADDR_STRING,
MyDBAdapter.KEY_TO_ADDR_STRING }, new int[] {
R.id.from, R.id.to }));
按预期创建列表。但是:有时这个列表的条目似乎消失了(有时候其中一些仍然可见)。每当我开始一项新活动并(立即)回到此活动时,这似乎就会发生,例如切换到主屏幕然后返回我的活动。但是,这只发生一次:当再次切换到“相同的其他”活动然后返回时,所有列表条目仍然可见。 我检查了列表值,看起来它们是从数据库中正确接收但有些看不见。
有人可以向我解释我做错了什么吗?
答案 0 :(得分:0)
getView是类似于Applet中的paint方法的方法,ok,getCount确定,绘制了多少数据,即在ListView中显示
在getCount()方法和getItem {int pos)中指定列表视图的大小,getItemId()
public int getCount() {
return array.length;
// ForInstance i had an array
//which contains 5 elements, i want that % elements in List, that's why i had specified the array.length here
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}