简体   繁体   English

Android-自定义列表视图适配器中的CheckBoxes怪异问题?

[英]Android - weird problem with CheckBoxes in custom listview adapters?

I am having a weird problem. 我有一个奇怪的问题。

I am working on a app which has one button and a listview with checkbox(one per list item) and functionality required that whenever i click on this button, checked items from list should be removed. 我正在开发一个应用程序,该应用程序具有一个按钮和一个带复选框的列表视图(每个列表项一个),并且功能要求每当我单击此按钮时,都应删除列表中的选中项。 I saved the state of checkbox in my custom ListView Adapter using Boolean type Arraylist and trying to access it in my main activity but the problem is whenever i click on Button its not removing the checked items instead its removing the items of exact amount as checked items from the bottom of the List. 我使用布尔类型Arraylist将复选框的状态保存在我的自定义ListView适配器中,并尝试在我的主要活动中访问它,但是问题是每当我单击Button时,它都不会删除选中的项目,而是删除了确切数量的项目作为选中的项目从列表的底部开始。

here is my custom adapter's code - 这是我的自定义适配器的代码-

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;
public ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
private ArrayList<String> itemSelected = new ArrayList<String>();
private ArrayList<TextView> ctv = new ArrayList<TextView>();
private int posi;
private String pkg;

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(row==null)
    {
        row = inflater.inflate(R.layout.item, parent, false);
        holder = new ViewHolder();
        holder.textView = (TextView)row.findViewById(R.id.text1);
        holder.imageView = (ImageView)row.findViewById(R.id.image);
        holder.checkBox = (CheckBox)row.findViewById(R.id.check);
        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);
    holder.textView.setTag(pkgName);


    holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton button, boolean b) {

                    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)
    {

    }

     return row;
}




public String getPkgName(int position)
{
    return itemSelected.get(position);
}
public void setItemChecked(boolean isChecked)
{

}
}

nd here is my onClick code - nd这是我的onClick代码-

public void onClick(View view)
{
    int size = icon.getCount();
    for(int i = size-1; i >= 0; i--)
    {
        if(icon.itemChecked.get(i))
        {
            list.remove(i);
        }
    }
    icon.notifyDataSetChanged();
}

and it also giving me an IOOBException sometime. 而且有时也会给我IOOBException。

Please help. 请帮忙。

Your onClick code is wrong, because after list.remove(i) you got size = size-1 , but you still assume that it's constant. 您的onClick代码是错误的,因为在list.remove(i)您得到size = size-1 ,但是您仍然认为它是常数。 You need to rewrite your delete loop. 您需要重写删除循环。 Plus you have to realize that, when you do remove and do nothing with your iteration index i , you skip 1 item (the one that was situated right after removed one). 另外,您还必须意识到,当您remove并且对迭代索引i不执行任何操作时,您将跳过1个项目(该项目位于删除的项目之后)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM