带有列表的 Android 对话框在滚动时使用自定义列表适配器随机丢失信息

标签 android eclipse dialog scroll listadapter

我创建了一个对话框并用一个使用自定义列表适配器的 ListView 填充它。它工作正常,但我注意到当列表足够长可以滚动时,来回滚动会导致我的一些列表项随机丢失一些数据。我注意到它也总是相同的列表项。例如,每个列表项都会有一个标题、图像和日期。当我滚动时,有些日期似乎消失了。当我开始对话时,它们总是在那里,一旦我滚动,它们总是消失。

奇怪的是,我的列表行由 2 行中的几个 TextView 组成,而只有最下面一行 TextView 消失了……有什么想法吗?

我的对话代码

 itemSendPickerDialog = new Dialog(this);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
   builder.setTitle("Select Item to Send");
    ListView lv = new ListView(this);
    Cursor c = mDbHelper.fetchItemsByDate(id);
    c.moveToFirst();

    int i = R.layout.send_item_menu_row;
    MyListAdapter ia = new MyListAdapter(this, mainListBackground, c, true);
    lv.setAdapter(ia);

    builder.setView(lv);
    itemSendPickerDialog = builder.create();
    itemSendPickerDialog.show();

还有我的自定义列表适配器类:

class MyListAdapter extends ResourceCursorAdapter {
    public MyListAdapter(Context context, int i, Cursor cursor, boolean...sending) {
        super(context, i, cursor);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView title = (TextView) view.findViewById(R.id.item_name);

        title.setText(cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_ITEM_TITLE)));

        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int width = display.getWidth();
        width = width - 150;
        ViewGroup.LayoutParams params = title.getLayoutParams();
        params.width = width;
        title.setLayoutParams(params);

        String cat = cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_ITEM_CATEGORY));
        if (cat.equalsIgnoreCase("trip notes")) {
            LinearLayout ll = (LinearLayout) view.findViewById(R.id.item_datetime_holder);
            ll.setVisibility(View.INVISIBLE);
        }
        TextView date = (TextView) view.findViewById(R.id.item_date);
        date.setText(cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_DEP_DATE)));

        TextView time = (TextView) view.findViewById(R.id.item_time);
        time.setText(cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_DEP_TIME)));

        ImageView iv = (ImageView) view.findViewById(R.id.image_icon);
        if (iv != null) {
            int index = cursor.getColumnIndex(TripsDbAdapter.KEY_ITEM_TYPE);

            String type = cursor.getString(index);
            if (type != null) {

            } else {
                type = "notes";
            }

            iv.setImageResource(getTypeResource(type));
        }

    }
}

最佳答案

我也遇到过这个问题... 您面临的问题是由于向上/向下滚动时 LIstView 回收了 View 。在您的情况下, ListView 为您提供了那些回收的 View ,您通过使它们不可见来更改了它们的属性。一些可能的解决方案可能是:

1) 当 cat.equalsIgnoreCase("trip notes")) 变为 true 时,您使某些 View 不可见。这种看不见的观点然后被回收并归还。回收的 View 仍然不可见(当它还给你时),所以你可以做的是每次在你的 ListAdapter 的开头使这个不可见的 View 可见。 您可以将此代码放在 bindView 方法的开头,您首先使布局可见,然后继续执行其余逻辑。(简而言之,显示的日期不会消失,只是不可见)。

2) 在您的适配器中覆盖 getViewTypeCount()。从你的代码 fragment 来看,你似乎有两种类型的行(一种是 R.id.item_datetime_holder 是不可见的,另一种是可见的),所以从这个方法返回 2(请做一些试验和错误)。这应该解决这个问题。

public int getViewTypeCount() {

        return 2;
    }

您将在此链接 http://logc.at/2011/10/10/handling-listviews-with-multiple-row-types/ 找到出色的解释

3) 您可以根据您的 if 条件扩充完全不同的布局。但是效率会差一些。

关于带有列表的 Android 对话框在滚动时使用自定义列表适配器随机丢失信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9507934/

相关文章:

javascript - 使用 Javascript 和 CSS 的 ReactJs 模态

windows - 是否有免费的 "Add Connection"或 "SQL Connection"对话框?

android - 在android中的edittext中使用spannable

android - 如何在 android gradle 中循环依赖项时获取当前风格?

android - 从 BroadcastReceiver 更新 fragment 的 TextView

java - 使用什么 Intent 将多任务列表从 Action Memo 应用程序发送到 S Planner 应用程序?

java - 如何从类路径中删除 JNA.jar?

java - Android,如何从try catch错误中显示对话框?

java - 如何在 tomcat 中查看 .class 文件的输出

dart - 如何在 ListTile 中添加 AlertDialog