checkbox in List being checked unexpected

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

checkbox in List being checked unexpected

Postby rayman » Tue Feb 16, 2010 7:15 pm

I have list of checkboxes in list binded by Custom simpleCurserAdapter.
in my Custom simpleCurserAdapter ive override the newview and binview with my modifications.
ive managed somehow to do multichoice..
wierd thing after i delete any item from my list , the first item's checkbox is being checked all of a sudden.. i know now what's happening with the cycling stuff, but how can i re-write my code so it will work properly
Thanks
code: my curser SimpleCursorAdapter class:


public class MyListCursorAdapter extends SimpleCursorAdapter

{
private Context context;
private int layout;

public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to)
{
super(context, layout, c, from, to);

this.context = context;

this.layout = layout;

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{

Cursor c = getCursor();

final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
CheckBox chkBoxBtn = (CheckBox) v.findViewById (R.id.deleteTwittChkBox);
if (chkBoxBtn != null)
{
chkBoxBtn.setChecked(false);
}

return v;

}

@Override
public void bindView(View v, Context context, Cursor c)
{

--binding view to my textsview in my items

//now it's the importat part:

CheckBox chkBoxBtn = (CheckBox) v.findViewById(R.id.deleteTwittChkBox);
if (chkBoxBtn != null)
{
chkBoxBtn.setId(Integer.valueOf(c.getString(c
.getColumnIndex(MyUsers.User._ID))));
chkBoxBtn.setOnClickListener(new OnItemClickListener(chkBoxBtn, v));
chkBoxBtn.setChecked(false);
}
}



//i couldnt find another way of doing this, but this is how i set listeners to my checkboxses

static ArrayList<String> checkedItemsList = new ArrayList<String>();

private class OnItemClickListener implements OnClickListener
{

private int mPosition;
private CheckBox chkBox;

OnItemClickListener(CheckBox mChkBox, View v)
{

chkBox = mChkBox;
chkBox.setChecked(false);
}


@Override
public void onClick(View v)
{
if (chkBox.isChecked())
{
checkedItemsList.add(String.valueOf(chkBox.getId()));
}
else
{
checkedItemsList.remove(String.valueOf(chkBox.getId()));
}

}




}

}


okie now this is code part from the ListActivity class which desbribes the button which deleting the checked Box items

OnClickListener btListener = new OnClickListener()
{
public void onClick(View view)
{

// long[] items = listView.getCheckItemIds();
int x = 0;
Uri myUri = Uri
.parse("content://com.idan.datastorageprovider/users");
String where = "_id" + "=?";
//here i am tatking all checkboxes which ive added from the adapter class
ArrayList<String> checkedItemsList = MySimpleCursorAdapter.checkedItemsList;
for (String itemID : checkedItemsList)
{
getContentResolver()
.delete(myUri, where, new String[] { itemID});
checkedItemsList.remove(itemID);
}


}
};

after i delete an item the bindview method is being called again, and when it reach to this part:
CheckBox chkBoxBtn = (CheckBox) v.findViewById(R.id.deleteTwittChkBox);
chkBoxBtn is getting NULL!! how come?? mybe here it's the prob? any idea? it's like all the rest of the views(textViews..) r being retrieved from the layout fine, but when it's get to the checkbox part then: v.findViewById(R.id.deleteTwittChkBox) getting null.. wierd thing!! –

thanks,
ray.
rayman
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Jan 11, 2010 11:26 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests