
在ListVIEw中,我尝试放置一个适配器(从基本适配器扩展)内容复选框.
列表视图布局:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" > <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 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>
我在我的视图中设置了适配器:
List = getListVIEw();List.setAdapter(new CatAdapter(this,ListCat));
我的适配器:
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,true); iv_cat.setimageBitmap(icon); return layoutItem; }} 当活动被推出时,列表将完美显示.我可以毫无问题地向上/向下滚动.但是,当我选中其中一个复选框并向下滚动时,选中“松散”其复选标记的框和列表中最新框的背景颜色已更改.
选中框时,会调用以下方法(在我的活动文件中):
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个元素.
我只想保存已检查的元素的“this.cat_ID_List”ID并更改所选项目的背景颜色.
非常感谢你的帮助!
解决方法 在你的getVIEw中你需要这样的东西(这段代码让你知道它在你的情况下是如何工作的):ListVIEw lv = ((ListActivity)context).getListVIEw();// Containing all check statesSparseBooleanArray sba = lv.getCheckedItempositions();CheckBox cb = (CheckBox) convertVIEw.findVIEwByID(R.ID.yourcheckBox);cb.setChecked(false);if(sba != null) if(sba.get(position)) cb.setChecked(true);
这就是你在活动方面所需要的.
你已经通过androID:在xml中使用selectMode或使用setChoiceMode将ListVIEw设置为多选模式.复选框应该是不可点击的,不可聚焦的.
您必须删除按钮上的onClick侦听器.无论你在onClick按钮上做什么,你都必须将该逻辑添加到ListActivtiy的onListItemClick.
总结以上是内存溢出为你收集整理的android:ListView中的复选框(所选元素的奇怪行为)全部内容,希望文章能够帮你解决android:ListView中的复选框(所选元素的奇怪行为)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)