机器人:在ListView复选框(所选元素的奇怪行为)机器人、所选、复选框、元素

2023-09-06 04:30:10 作者:脑子不在服务区

我发现here和这里类似的问题,但问题是稍有不同。

I found here and here similar issues, but the problem is slightly different.

在一个ListView,我尽量把适配器(从底座适配器扩展),其内容复选框。

In a ListView, I try to put an adapter (extends from base Adapter) which contents checkbox.

列表视图布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/mainLayout"
 >

    <Button
        android:id="@+id/close_cat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="buttonClick"
        android:text="@string/back" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

列表的内容的XML:

XML of list's elements:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TableLayout
        android:id="@+id/blocCheck"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="25px"
        android:layout_marginTop="25px"
        android:background="@color/black" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="true"
            android:paddingLeft="4sp" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:paddingLeft="20sp" >

                <ImageView
                    android:id="@+id/iv_cat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/category"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:layout_marginLeft="15dip"
                    android:text="tfgldkjhglf"
                    android:textSize="20sp" />
            </LinearLayout>

            <CheckBox
                android:id="@+id/check_cat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginRight="10sp"
                android:onClick="MyHandler" />
        </TableRow>
    </TableLayout>

</LinearLayout>

我在我看来设置适配器:

I set adapter in my view :

list = getListView();
list.setAdapter(new CatAdapter(this, listCat));

我的适配器:

My adapter:

public class CatAdapter extends BaseAdapter {

    private LayoutInflater  mInflater;
    private List<CatAdapterObject> mListAppInfo;
    private Context mContext;


    /**
     * Constructor
     * @param context
     * @param data
     * @param resource
     * @param from
     * @param to
     */
    public CatAdapter (Context context, List<CatAdapterObject> list){   
        mContext = context;
        mListAppInfo = list;
        mInflater = LayoutInflater.from(mContext);      
    }

    /**
     * number of elements
     */
    @Override
    public int getCount() {
        return mListAppInfo.size();
    }

    /**
     * get an item in the list
     */
    @Override
    public Object getItem (int position){
        return mListAppInfo.get(position).getId();
    }

    /**
     * get id of the selected item
     */
    @Override
    public long getItemId(int position) {
        return mListAppInfo.get(position).getId();
    }

    /**
     * get the view
     */
    @Override
    public View getView (int position, View convertView, ViewGroup parent){

        LinearLayout layoutItem;

        if (convertView == null) {
            layoutItem = (LinearLayout) mInflater.inflate(R.layout.category,    parent, false);
        } else {
            layoutItem = (LinearLayout) convertView;
        }

        // get the current category
        CatAdapterObject entry =  mListAppInfo.get(position);

        //view setting
        TextView category = (TextView) layoutItem.findViewById(R.id.category);
        CheckBox cb         = (CheckBox) layoutItem.findViewById(R.id.check_cat);
        ImageView iv_cat    = (ImageView) layoutItem.findViewById(R.id.iv_cat);

        //elements setting
        category.setText(entry.getName());

        //checkbox
        cb.setChecked(entry.isChecked());
        cb.setTag(position);

        //picture
        Bitmap icon = Utils.getResizedBitmap(BitmapFactory.decodeResource(mContext.getResources(),entry.getPict()),45,45,true);     
        iv_cat.setImageBitmap(icon);

        return layoutItem;

    }
}

在活动lauched,完美显示的列表。我可以/下没有任何问题向上滚动。但是,当我检查的复选框中的一个,然后向下滚动,箱这是最好的宽松的对勾,并在列表中最新的框的背景颜色改变。

When activity is lauched, list is displayed perfectly. I can scroll up/down without any problem. However, when I check one of the checkboxes and scroll down, box which was checked "loose" its check mark and the background color of the latest box in the list changed.

当检查中,下面的方法被调用(在我的活动文件):

When checking a box, below method is called (In my activity file):

public void MyHandler(View v) {

        //クリックされたcheckbox
        cb = (CheckBox) v;

        Log.d("CHECKBOX","before :"+Utils.implode(",",this.cat_id_list));

        //get position in the list
        Integer position = Integer.parseInt(cb.getTag().toString());

        //instance of the view (list's child)
        View o = list.getChildAt(position).findViewById(R.id.blocCheck);

        // check if box is checked
        if (cb.isChecked()) { 
            o.setBackgroundResource(R.color.pink);

            this.cat_id_list.add(position.toString());

        // 背景色を替え、リストから削除
        } else { 

            o.setBackgroundResource(R.color.black);

            Collections.sort(this.cat_id_list);
            int index = Collections.binarySearch(this.cat_id_list,position.toString());
            this.cat_id_list.remove(index);

        }

        Log.d("CHECKBOX","after :"+Utils.implode(",",this.cat_id_list));
    }

有我的列表中8种元素,但是当我检查mChildrenCount列表中,只有5元。

There are 8 elements in my list but when I check the "mChildrenCount" of the list, there are only 5 elements.

我只是想保存的元素至极的this.cat_id_list标识进行了检查,并更改所选项目的背景色。

I just want to save in the "this.cat_id_list" id of elements wich were checked and change background color of selected item.

我会AP preciate您的帮助!

I would appreciate your help !

推荐答案

在你的getView你要这样的事情(这code是给你一个想法将如何工作在你的情况):

In your getView you have to something like this (this code is give you an idea how it will work in your case):

ListView lv = ((ListActivity)context).getListView();
// Containing all check states
SparseBooleanArray sba = lv.getCheckedItemPositions();

CheckBox cb = (CheckBox) convertView.findViewById(R.id.yourcheckbox);

cb.setChecked(false);

if(sba != null)
  if(sba.get(position))
     cb.setChecked(true);

这是说这就是你需要在活动方。

That said this what you need on the Activity side.

您必须让你的ListView到多选择模式由机器人:XML或使用setChoiceMode choiceMode。你复选框应该不点击的,不能成为焦点。

You have make your ListView to multi-selection mode by android:choiceMode in xml or using setChoiceMode. You check box should be non-clickable and non-focusable.

您必须删除您的onClick监听器上的按钮。无论你在按钮的onClick做,你必须是逻辑添加到ListActivtiy的onListItemClick。

You have to remove your onClick listener on buttons. Whatever you doing in onClick of button, you have to add that logic to the onListItemClick of ListActivtiy.