android– 自定义ListView,复选框检查未选中的项目

android– 自定义ListView,复选框检查未选中的项目,第1张

概述我有一个BaseAdapterforListView里面的片段,如下所示:publicclassSelectionMucListAdapterextendsBaseAdapter{privateLayoutInflaterinflater=null;TypefacetitleFace;ArrayList<UsersData>innerList=newArrayList<UsersData>();publ

我有一个BaseAdapter for ListVIEw里面的片段,如下所示:

public class SelectionMuclistadapter extends BaseAdapter {    private LayoutInflater inflater = null;    Typeface TitleFace;    ArrayList<UsersData> innerList = new ArrayList<UsersData>();    public SelectionMuclistadapter(ArrayList<UsersData> users) {        inflater = (LayoutInflater) mainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        TitleFace = Typeface.createFromAsset(mainActivity.getAssets(), "Fonts/bradybun.ttf");        innerList = users;    }    @OverrIDe    public int getCount() {        return innerList.size();    }    @OverrIDe    public Object getItem(int position) {        return position;    }    @OverrIDe    public long getItemID(int ID) {        return ID;    }    @OverrIDe    public VIEw getVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) {        VIEwHolder vIEwHolder;        if (convertVIEw == null) {            convertVIEw = inflater.inflate(R.layout.muc_List_item, null);            vIEwHolder = new VIEwHolder();            vIEwHolder.image = (ImageVIEw)convertVIEw.findVIEwByID(R.ID.item_image);            vIEwHolder.check = (CheckBox)convertVIEw.findVIEwByID(R.ID.cb_choose_user);            vIEwHolder.Title = (TextVIEw)convertVIEw.findVIEwByID(R.ID.item_text_1);            vIEwHolder.rating=(ratingbar)convertVIEw.findVIEwByID(R.ID.item_text_2);            convertVIEw.setTag(vIEwHolder);        }else{            vIEwHolder = (VIEwHolder)convertVIEw.getTag();        }                 vIEwHolder.Title.setTypeface(TitleFace);        Bitmap avatar = innerList.get(position).getUserAvatar();//UsersManager.getInstance().getUsers().get(position).getUserAvatar();        if(avatar != null)            vIEwHolder.image.setimageBitmap(avatar);        vIEwHolder.Title.setText(innerList.get(position).getUsername());        vIEwHolder.rating.setrating((float)Double.parseDouble(innerList.get(position).getrating()));        vIEwHolder.check.setonCheckedchangelistener(new OnCheckedchangelistener() {            @OverrIDe            public voID onCheckedChanged(Compoundbutton cb, boolean isChecked) {                if(cb.isChecked()) {// if this item is checked then add to List                    Log.v("CheckBox", "pos: "+position+" checked");                    if(!choices.contains(innerList.get(position))) {                        choices.add(innerList.get(position));                    }                } else {// if its unchecked then remove from List if exist                    Log.v("CheckBox", "pos: "+position+" checked");                    if(choices.contains(innerList.get(position))) {                        choices.remove(innerList.get(position));                    }                }                isChecked = cb.isChecked();            }        });        vIEwHolder.check.setTag(position);        return convertVIEw;    }}static class VIEwHolder{    ImageVIEw image;    CheckBox check;    TextVIEw Title;    ratingbar rating;}class CheckState{    boolean isChecked;    UsersData usersData;    public UsersData getUsersData() {        return usersData;    }    public voID setUsersData(UsersData usersData) {        this.usersData = usersData;    }    public boolean isChecked() {        return isChecked;    }    public voID setChecked(boolean isChecked) {        this.isChecked = isChecked;    }}

我有20个列表项,问题是当我在索引[0]和[1]检查前两个项目时,不知怎的,当我向下滚动时,它还会自动检查索引[9]和[10]中的其他项目名单.如果我检查了其他项目也会发生同样的情况,并且每次我向下滚动列表时都会以相同的模式重复检查.

我其实不知道发生了什么,所以我想问一下这里是否有人可以阻止这种混乱.谢谢!

解决方法:

您的自定义适配器必须实现Compoundbutton.OnCheckedchangelistener.使用SparseBooleanArray.

然后

 cb.setChecked(mCheckStates.get(position, false)); // cb is checkBox cb.setonCheckedchangelistener(this);

然后使用选中状态将文本设置为复选框

  public boolean isChecked(int position) {    return mCheckStates.get(position, false);}public voID setChecked(int position, boolean isChecked) {    mCheckStates.put(position, isChecked);}public voID toggle(int position) {    setChecked(position, !isChecked(position));}@OverrIDepublic voID onCheckedChanged(Compoundbutton buttonVIEw,    boolean isChecked) { mCheckStates.put((Integer) buttonVIEw.getTag(), isChecked);    }

关于这个话题的讨论@

https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M

例:

使用VIEwHolder.以下示例不使用VIEw Holder.

屏幕底部有一个按钮.当您选中复选框时,单击底部的按钮时,选中的行的项目将显示在Toast中.

使用以下内容并根据您的要求进行修改.

public class MainActivity extends Activity implementsAdapterVIEw.OnItemClickListener {    int count;private CheckBoxAdapter mCheckBoxAdapter;String[] GENRES = new String[] {    "Action", "Adventure", "Animation", "Children", "Comedy","documentary", "Drama",    "Foreign", "History", "Independent", "Romance", "Sci-Fi","Television", "Thriller"};@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    final ListVIEw ListVIEw = (ListVIEw) findVIEwByID(R.ID.lv);    ListVIEw.setItemsCanFocus(false);    ListVIEw.setTextFilterEnabled(true);    ListVIEw.setonItemClickListener(this);    mCheckBoxAdapter = new CheckBoxAdapter(this, GENRES);           ListVIEw.setAdapter(mCheckBoxAdapter);    button b= (button) findVIEwByID(R.ID.button1);    b.setonClickListener(new OnClickListener()    {        @OverrIDe        public voID onClick(VIEw v) {            // Todo auto-generated method stub            StringBuilder result = new StringBuilder();            for(int i=0;i<mCheckBoxAdapter.mCheckStates.size();i++)            {                if(mCheckBoxAdapter.mCheckStates.get(i)==true)                {                    result.append(GENRES[i]);                    result.append("\n");                }            }            Toast.makeText(MainActivity.this, result, 1000).show();        }    });   }public voID onItemClick(AdapterVIEw parent, VIEw vIEw, intposition, long ID) {    mCheckBoxAdapter.toggle(position);}class CheckBoxAdapter extends ArrayAdapter implements Compoundbutton.OnCheckedchangelistener{  private SparseBooleanArray mCheckStates;   LayoutInflater mInflater;    TextVIEw tv1,tv;    CheckBox cb;    String[] gen;    CheckBoxAdapter(MainActivity context, String[] genres)    {        super(context,0,genres);        mCheckStates = new SparseBooleanArray(genres.length);        mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        gen= genres;    }    @OverrIDe    public int getCount() {        // Todo auto-generated method stub        return gen.length;    }    @OverrIDe    public Object getItem(int position) {        // Todo auto-generated method stub        return position;    }    @OverrIDe    public long getItemID(int position) {        // Todo auto-generated method stub        return 0;    }    @OverrIDe    public VIEw getVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) {        // Todo auto-generated method stub        VIEw vi=convertVIEw;        if(convertVIEw==null)         vi = mInflater.inflate(R.layout.checkBox, null);          tv= (TextVIEw) vi.findVIEwByID(R.ID.textVIEw1);         cb = (CheckBox) vi.findVIEwByID(R.ID.checkBox1);         tv.setText("name :"+ gen [position]);         cb.setTag(position);         cb.setChecked(mCheckStates.get(position, false));        cb.setonCheckedchangelistener(this);        return vi;    }     public boolean isChecked(int position) {            return mCheckStates.get(position, false);        }        public voID setChecked(int position, boolean isChecked) {            mCheckStates.put(position, isChecked);        }        public voID toggle(int position) {            setChecked(position, !isChecked(position));        }    @OverrIDe    public voID onCheckedChanged(Compoundbutton buttonVIEw,            boolean isChecked) {         mCheckStates.put((Integer) buttonVIEw.getTag(), isChecked);        }}}
总结

以上是内存溢出为你收集整理的android – 自定义ListView,复选框检查未选中的项目全部内容,希望文章能够帮你解决android – 自定义ListView,复选框检查未选中的项目所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1106883.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存