Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid : checkboxes in a ListView ( strange behaviour of selected element)
    primarykey
    data
    text
    <p>I found <a href="https://stackoverflow.com/questions/3606530/listview-scroll-to-the-end-of-the-list-after-updating-the-list">here</a> and <a href="https://stackoverflow.com/questions/5444355/android-listview-with-checkbox-problem">here</a> similar issues, but the problem is slightly different.</p> <p>In a ListView, I try to put an adapter (extends from base Adapter) which contents checkbox.</p> <p>List view layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/mainLayout" &gt; &lt;Button android:id="@+id/close_cat" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="buttonClick" android:text="@string/back" /&gt; &lt;ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>XML of list's elements:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;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" &gt; &lt;TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" android:baselineAligned="true" android:paddingLeft="4sp" &gt; &lt;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" &gt; &lt;ImageView android:id="@+id/iv_cat" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;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" /&gt; &lt;/TableRow&gt; &lt;/TableLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>I set adapter in my view :</p> <pre><code>list = getListView(); list.setAdapter(new CatAdapter(this, listCat)); </code></pre> <p>My adapter:</p> <pre><code>public class CatAdapter extends BaseAdapter { private LayoutInflater mInflater; private List&lt;CatAdapterObject&gt; mListAppInfo; private Context mContext; /** * Constructor * @param context * @param data * @param resource * @param from * @param to */ public CatAdapter (Context context, List&lt;CatAdapterObject&gt; 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; } } </code></pre> <p>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.</p> <p>When checking a box, below method is called (In my activity file):</p> <pre><code>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)); } </code></pre> <p>There are 8 elements in my list but when I check the "mChildrenCount" of the list, there are only 5 elements.</p> <p>I just want to save in the "this.cat_id_list" id of elements wich were checked and change background color of selected item.</p> <p>I would appreciate your help !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload