0

I have a ListView in my app which has a custom adapter. I have a ImageView, a CheckBox and a TextView in my list view and i have one button in my app which should delete Checked items from the list on onClick event but the problem is - It does not matter which items i am selecting but it's always deleting the items from the botton of the list.

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 -

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();
}

please help!!!!!

And when i removed notifyDataSetChanged from onClick and reload the list manually again its working exactly i want it to work so there is some problem in NotyfyDataSetChanged. Please help.

Varundroid
  • 9,135
  • 14
  • 63
  • 93
  • Are you sure that `icon.itemChecked.get(i)` is returning `true`? – Tanmay Mandal Apr 14 '11 at 07:37
  • Hello Varundroid again, what are your `list` and `icon` classes? icon seems to be list adapter, and list? – ernazm Apr 14 '11 at 07:39
  • @Tanmay if it wont return true then the item will never be deleted bro but my problem is wrong items are getting deleted not the right once so it means there is some index problem. @user63.... ArrayList> list = new ArrayList>(); is my list in which i have nearly 10-15 items. please help guys i am stuck at this part since last two days. :'( – Varundroid Apr 14 '11 at 07:47
  • As java `ArrayList` is NOT synchronized, that may be the reason.Try to use `List` instead of `ArrayList` – Tanmay Mandal Apr 14 '11 at 09:33

2 Answers2

0

You can use notifyDataSetChanged method of your adapter, but keep in mind some points that are stated here (see accepted answer) notifyDataSetChanged example

Community
  • 1
  • 1
Yekmer Simsek
  • 4,102
  • 3
  • 21
  • 19
  • I see, look at the accepted answer on the page, you must implement some other methods that are missing. – Yekmer Simsek Apr 14 '11 at 08:12
  • hmmmm you are right there is some problem in my notifyDataSetChanged. It is not notifying my Adapter in a way it should. Any idea why? and i have gone through that thread and tried some solutions but it didn't work. i don't know why notifyDataSetCHanged is not working right. – Varundroid Apr 14 '11 at 10:08
0

did you put a log statement in onCheckedChanged method and see what position is being set ? My guess is that you shouldn't use the position argument in that manner. One way to do so could be to use holder.checkBox.setTag(new Integer(position)) and in the onCheckedChanged method use int posClicked = ((Integer)button.getTag()).intValue() and then use this instead of position

pankajagarwal
  • 13,462
  • 14
  • 54
  • 65