我已经通过扩展SimpleCursorAdapter创建的自定义的ListView。 其结果是IMAGE + CheckedTextView(文本+复选框)。

当我长单击一个项目,一切工作正常 - 我得到的点击的项目的权限ID和细节

当我尝试标志的项目作为托运但它会检查错误复选框时出现问题。

例如:我的名单上的9个项目,分选1-9。如果我点击的listItem 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);
                }

            }
        });

感谢任何帮助!

让我知道,如果你需要我张贴更多的代码。

谢谢!

顺便说一句,如果我点击不止一台...的PARTY继续......没有明显的顺序...

编辑:适配器代码

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);
    }
}
有帮助吗?

解决方案

梅拉是正确的 - 问题与ListView控件被重用你的意见的方式做。这并不是因为如果存在的CheckedTextView对象9分的情况下,每一个视图。相反,还有的是在所有的行重用的一个。因此,你可以不依赖于CheckedTextView对象持有的项目是否被选中的状态。你需要一些额外的数据结构来保存一个给定的行是否被选中例如,

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

其中当且仅当ith行应检查ith元件是真实的。那么你itemClickListener内:

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

这应该解决您的问题。一种替代方法将是移动的行是否被选中或不进域对象,该行表示的逻辑。这将是我的偏好。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top