来自其他活动时 ListView 为空

发布于 2024-10-16 07:20:36 字数 2442 浏览 4 评论 0原文

我有一个扩展“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一页 2024-10-23 07:20:36

getView是类似于Applet中的paint方法的方法,好吧,getCount决定了绘制多少数据,即显示在ListView中

在getCount()中指定List View的大小方法和 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;
}

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()

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文