代码之家  ›  专栏  ›  技术社区  ›  Lior Iluz

安卓点击列表项检查错误复选框

  •  8
  • Lior Iluz  · 技术社区  · 14 年前

    我通过扩展SimpleCursorAdapter创建了一个自定义列表视图。

    当我长时间点击一个项目时,一切都很好——我得到了正确的ID和点击项目的详细信息。

    当我试图将一个项目标记为选中,但它选中了错误的复选框时,就会出现问题。

    我的单子上有9项,按1-9排序。如果单击列表项1,将选中第9行上的复选框。如果单击第4项,则选中第6行上的复选框,如果单击中间行,则选中该复选框。

    很明显我遗漏了一些东西:)

    这是听众:

    lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    CheckedTextView markedItem = (CheckedTextView) view.findViewById(R.id.btitle);
    
                    if (!markedItem.isChecked()) {
                        markedItem.setChecked(true);
                    } else {
                        markedItem.setChecked(false);
                    }
    
                }
            });
    

    感谢您的帮助!

    谢谢您!

    顺便说一下,如果我点击多个。。。党继续…没有明显的顺序。。。

    编辑:适配器代码

    public class ImageCursorAdapter extends SimpleCursorAdapter {
    
        private Cursor c;
        private Context context;
    
        private String url;
        private TextView bUrl;
    
        public ImageCursorAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {
            super(context, layout, c, from, to);
            this.c = c;
            this.context = context;
        }
    
        public View getView(int pos, View inView, ViewGroup parent) {
            View v = inView;
            if (v == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.image_list, null);
            }
    
            this.c.moveToPosition(pos);
    
            final TextView bTitle = (TextView) v.findViewById(R.id.btitle);
            String bookmark = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.TITLE));
    
    
            byte[] favicon = this.c.getBlob(this.c.getColumnIndex(Browser.BookmarkColumns.FAVICON));
    
            if (favicon != null) {
                ImageView iv = (ImageView) v.findViewById(R.id.bimage);
                iv.setImageBitmap(BitmapFactory.decodeByteArray(favicon, 0, favicon.length));
            }
            bTitle.setText(bookmark);
    
            return (v);
        }
    }
    
    1 回复  |  直到 14 年前
        1
  •  11
  •   I82Much    13 年前

    Mayra是对的-这个问题与ListView重用视图的方式有关。并不是说有9个 CheckedTextView 对象,每个视图一个。相反,在所有行中都有一个被重用。因此,您不能依赖CheckedTextView对象来保存项目是否被选中的状态。您需要一些额外的数据结构来保存是否检查了给定的行,例如,

    ArrayList<Boolean> checkedStates = new ArrayList<Boolean>();
    

    ith 如果 伊思

    lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                boolean currentlyChecked = checkedStates.get(position);
                checkedStates.set(position, !currentlyChecked);
                // Refresh the list
            }
        });
    

    然后在视图代码中:

    public View getView(int pos, View inView, ViewGroup parent) {
        View v = inView;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.image_list, null);
        }
    
        this.c.moveToPosition(pos);
    
        final TextView bTitle = (TextView) v.findViewById(R.id.btitle);
        String bookmark = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.TITLE));
    
    
        byte[] favicon = this.c.getBlob(this.c.getColumnIndex(Browser.BookmarkColumns.FAVICON));
    
        if (favicon != null) {
            ImageView iv = (ImageView) v.findViewById(R.id.bimage);
            iv.setImageBitmap(BitmapFactory.decodeByteArray(favicon, 0, favicon.length));
        }
        bTitle.setText(bookmark);
    
    
        // Change the state of the checkbox to match that of the row's checked state.
        // This check box item is reused for every row, so we need to reset its state each
        // time the row is rendered.
        CheckedTextView markedItem = (CheckedTextView) view.findViewById(R.id.btitle);
        markedItem.setChecked(checkedStates.get(pos));
    
    
        return (v);
    }