【问题标题】:Android Eclipse ListView in AlertDialog hiding certain textviews when scrolling滚动时AlertDialog中的Android Eclipse ListView隐藏某些文本视图
【发布时间】:2012-03-06 14:27:50
【问题描述】:

我在 alertDialog 中有一个列表视图。我使用自定义 resourceCursorAdapter 填充我的列表视图,每行包含一个标题 textView、日期 textView 和图像。

一切正常,除了看起来当您滚动列表视图时,某些行中的一些日期文本视图消失了。我已经弄清楚为什么它们消失了,只是不知道如何解决它......有些项目并不意味着显示日期,所以我在 bindview 上有一个 if 语句来确定是否应该显示日期。这适用于警报对话框之外的任何列表,但它会使一些本应显示的日期消失。有谁知道为什么会这样?

我的警报对话框和列表视图代码:

itemSendPickerDialog = new Dialog(this);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Select Item to Send");

        ListView lv = new ListView(this);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) {


                }
            });
        AlertDialog sendOptionAlert = sendOptionBuilder.create();

                         sendOptionAlert.show();

                    System.out.println("ID is: " + iid + " and position is: " + position);
                    itemSendPickerDialog.dismiss();
                    }
            });
        Cursor c = mDbHelper.fetchItemsByDate(id);
        c.moveToFirst();

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

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

还有我的自定义 listAdapter:

class MyListAdapter extends ResourceCursorAdapter {
    public MyListAdapter(Context context, int i, Cursor cursor) {
        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));
        }

    }

    public static int getTypeResource(String type) {
        if (type.equalsIgnoreCase("flight")) {
            return R.drawable.airplaneicon;
        }
        if (type.equalsIgnoreCase("boat/ship")) {
            return R.drawable.boatshipicon;
        }
        if (type.equalsIgnoreCase("bus")) {
            return R.drawable.busicon;
        }
        if (type.equalsIgnoreCase("rail")) {
            return R.drawable.railicon;
        }
        if (type.equalsIgnoreCase("auto or other rentals")) {
            return R.drawable.caricon;
        }
        if (type.equalsIgnoreCase("other transportation")) {
            return R.drawable.othertransicon;
        }
        if (type.equalsIgnoreCase("hotel")) {
            return R.drawable.hotelicon;
        }
        if (type.equalsIgnoreCase("resort")) {
            return R.drawable.resorticon;
        }
        if (type.equalsIgnoreCase("bed & breakfast")) {
            return R.drawable.bandbicon;
        }
        if (type.equalsIgnoreCase("camp ground")) {
            return R.drawable.campgroundicon;
        }
        if (type.equalsIgnoreCase("vacation rental")) {
            return R.drawable.vacationrentalicon;
        }
        if (type.equalsIgnoreCase("other lodging")) {
            return R.drawable.otherlodgingicon;
        }
        if (type.equalsIgnoreCase("rail")) {
            return R.drawable.railicon;
        }
        if (type.equalsIgnoreCase("cruise")) {
            return R.drawable.cruiseicon;
        }
        if (type.equalsIgnoreCase("tour")) {
            return R.drawable.touricon;
        }
        if (type.equalsIgnoreCase("dining")) {
            return R.drawable.diningicon;
        }
        if (type.equalsIgnoreCase("spa")) {
            return R.drawable.spaicon;
        }
        if (type.equalsIgnoreCase("class")) {
            return R.drawable.classicon;
        }
        if (type.equalsIgnoreCase("other activity")) {
            return R.drawable.eventicon;
        } else {
            return R.drawable.notesicon;
        }
    }

}

使文本消失的具体代码在bindview override中:

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);
        }

【问题讨论】:

    标签: android eclipse android-listview textview android-alertdialog


    【解决方案1】:

    ListView 中的视图被回收,当行在底部消失时,它在列表顶部出现。它不是重新创建,而是这样的优化。在bindView() 中,您只是设置当前值。

    您忘记了将视图设置为可见。

        LinearLayout ll = (LinearLayout) view.findViewById(R.id.item_datetime_holder);
        if (cat.equalsIgnoreCase("trip notes")) {   
            ll.setVisibility(View.INVISIBLE);
        } else {
            ll.setVisibility(View.VISIBLE);
        }
    

    值得一看:http://www.google.com/events/io/2010/sessions/world-of-listview-android.html

    【讨论】:

    • 最优秀。非常感谢您的回答和参考阅读!我没有意识到列表项是按原样回收的。虽然有一个问题......这解决了我的问题,但为什么我没有使用相同的代码遇到同样的问题,但在警报对话框中没有列表视图?所有的文本视图都可以这样工作。我确实在调用 bindview 方法时进行了测试并打印了出来,并且在 alertdialog 中重复调用了它(滚动时),但最初不是在一个中时(即使在滚动时)调用了几次。
    • 如果适配器相同,那么它的工作方式应该相似。也许不同的数据?
    猜你喜欢
    • 2014-10-24
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多