来自其他活动时 ListView 为空
我有一个扩展“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 }));
列表按预期创建。但是:有时此列表中的条目似乎消失了(有时其中一些条目仍然可见)。当我开始一项新活动并(立即)返回到该活动时,例如更改到主屏幕然后返回到我的活动时,似乎每次都会发生这种情况。但是,这种情况只发生一次:当再次切换到“相同的其他”活动然后返回时,所有列表条目仍然可见。 我检查了列表值,似乎它们是从数据库正确接收的,但有些不可见。
有人可以向我解释我做错了什么吗?
I have a class that extends a `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;
}
}
It's used that way:
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 }));
The list is created as expected. But: Sometimes the entries of this list seem to disappear (sometimes a few of them a still visible). This seems to happen each time, when I'm starting a new activity and (instantly) come back to this activity, e.g. changing to the home screen and then returning back to my activity. But, this happens only once: When switching again to the 'same other' activity and then come back, all list entries are still visible.
I checked the list values and it seems like they're received correctly from the database but some kind of invisible.
Can someone explain to me what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getView是类似于Applet中的paint方法的方法,好吧,getCount决定了绘制多少数据,即显示在ListView中
在getCount()中指定List View的大小方法和 getItem{int pos)、getItemId()
getView is the method which is similar to paint method in Applet, ok, getCount determines, how much datas are painted i.e., displayed in your ListView
Specify the size of the List View in getCount() method and getItem{int pos), getItemId()