Android的 - 无法检查,因为回收问题自定义列表视图的所有复选框? [英] Android - Unable to check all the CheckBoxes in a custom listview because of recycling issue?

查看:99
本文介绍了Android的 - 无法检查,因为回收问题自定义列表视图的所有复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的ListView适配器与ImageView的,TextView的和一个复选框。我也有一个按钮,在我的主要布局的复选框(不是在列表视图)。

I have a custom listview adapter with a imageview, textview and a checkbox. and i also have a button and a checkbox in my main layout(not in listview).

什么在这里,我要的是检查所有这些列表视图复选框一次当我检查我的主要布局的复选框。

What here i want is to check all these listview checkboxes at once when i check my main layout's checkbox.

和我节约了这些复选框中的布尔类型的ArrayList的状态,所以我可以检查这些复选框,当我逐一检查,但到目前为止,我没有找到一个办法一下子,我可以检查所有这些复选框在我的主要布局的复选框的OnCheckedChanged事件中使用。

And i am saving the state of these checkbox in boolean type arraylist so i am able to check these checkboxes when i check one by one but so far i did not find a single way to check all these checkbox at once which i can use in my main layout's checkbox's OnCheckedChanged event.

我试着一些技巧,但因为ListView的回收程序会检查只有那些当前可见的,当我向下滚动到我的名单,设置为未选中上述所有项目和一些随机的项目得到了检查的项目。

i tried few techniques but because of ListView's recycling procedure it checks only those items which are currently visible and as i scroll down to my list, all the above items set to unchecked and some random items got checked.

如何渡过这个循环的事情,以及如何在一次检查所有这些复选框????任何意见?解?想法?

how to get over this recycle thing and how to check all these checkboxes at once???? any advise? solution? idea?

下面是我的自定义适配器的code: -

here is a code of my Custom Adapter : -

public class IconAdapter extends BaseAdapter
{ 
private Activity activity;
private Object[] data;
private ArrayList<HashMap<String,String>> listItems;
public static LayoutInflater inflater = null;
private PackageManager pm;
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
private ArrayList<String> itemSelected = new ArrayList<String>();
private ArrayList<CheckBox> ctv = new ArrayList<CheckBox>();

public IconAdapter(Activity a, ArrayList<HashMap<String,String>> items)
{
    activity = a;
    listItems = items;
    data = items.toArray();
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    pm = a.getPackageManager();
    for(int i = 0; i < items.size(); i++)
    {
        itemChecked.add(i,false);
    }
    for(int i = 0; i < items.size(); i++)
    {
        itemSelected.add(i," ");
    }

}

public int getCount() {
    return listItems.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView textView;
    public ImageView imageView;
    public CheckBox checkBox;
}

public View getView(final int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    ViewHolder holder;
    if(convertView==null)
    {
        row = inflater.inflate(R.layout.item, parent, false);
        holder = new ViewHolder();
        holder.textView = (TextView)row.findViewById(R.id.text1);
        holder.checkBox = (CheckBox)row.findViewById(R.id.check);
        holder.imageView = (ImageView)row.findViewById(R.id.image);
        holder.checkBox.setTag(position);
        row.setTag(holder);
    }
    else
    {
        holder = (ViewHolder)row.getTag();
    }


    String s = data[position].toString();
    String[] tokens = s.split(",");
    String[] mToken = tokens[0].split("=");
    String taskName = mToken[1];
    String[] mTokens = tokens[1].split("=");
    final String pkgName =  mTokens[1].substring(0, (mTokens[1].length() - 1));


    holder.textView.setText(taskName);



    ctv.add(holder.checkBox);
    holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton button, boolean b) {
            //int posClicked = ((Integer)button.getTag());
            if(b)
            {
                itemChecked.set(position, true);
                itemSelected.set(position, pkgName);

            }
            else
            {
                itemChecked.set(position,false);

            }
        }
    });





    holder.checkBox.setChecked(itemChecked.get(position));




    try{
        Drawable icon =   pm.getApplicationIcon(pkgName);
        holder.imageView.setImageDrawable(icon);
    }
    catch (PackageManager.NameNotFoundException ne)
    {

    }
     row.setId(position);
     return row;
}

public boolean isChecked(int position)
{
    return itemChecked.get(position);
}
public String getPkgName(int position)
{
    return itemSelected.get(position);
}
public void removeItem(int position)
{
    listItems.remove(position);
}




public void setItemChecked(boolean isChecked)
{
    if(isChecked)
    {
        for(int i = 0; i < ctv.size(); i++)
        {

            ctv.get(i).setChecked(true);
        }
    }
    else
    {
        for(int i = 0; i < ctv.size(); i++)
        {

            ctv.get(i).setChecked(false);
        }
    }
}

,你可以在这个适配器的末端看到我做setItemChecked功能(),但这个功能也是循环的过程作为其唯一的检查可见项目的受害者,因为我向下滚动,它开始行为不端。

as you can see in the end of this adapter i made a function setItemChecked() but this function is also a victim of recycle process as its only checking the visible item and as i scroll down it starts misbehaving.

任何帮助将是巨大的。

感谢。

推荐答案

今天早上,我读到这样一个问题的答案,他们建议我们把 holder.checkBox.setChecked(itemChecked.get(位置)); holder.checkBox.setOnCheckedChangeListener

This morning I read an answer of an issue like this, and they recommend we put holder.checkBox.setChecked(itemChecked.get(position)); before holder.checkBox.setOnCheckedChangeListener

因此​​, getView 功能将被改写这样的:

So, getView function will be rewritten like this:

public View getView(....)
{
    ...
    holder.checkBox.setChecked(itemChecked.get(position));///move to here
    holder.checkBox.setOnCheckedChangeListener(...);
    ...
)

这篇关于Android的 - 无法检查,因为回收问题自定义列表视图的所有复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆