I have implemented a custom listview in which I have two textviews and checkbox. These are encapsulated into a linear layout on whoms click I check/uncheck the checkbox. The problem I am facing is that when I click the linear layout, it checks the checkbox but it also checks other checkboxes on a fixed pattern i.e every 7-8 rows. I read many problems regarding it and found out that implementing a view holder would solve the problem but that's also not working for me. Following is my code:
CustomListAdapter:
public class ContactsListAdapter extends BaseAdapter{
private Context context;
private List<ContactInfo> contacts;
private List<ContactInfo> selectedContacts;
public ContactsListAdapter(Context context, List<ContactInfo> contacts) {
this.context = context;
this.contacts = contacts;
selectedContacts = new ArrayList<ContactInfo>();
}
public List<ContactInfo> getSelectedContacts() {
return selectedContacts;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return contacts.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return contacts.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return Long.parseLong(contacts.get(position).id);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.contacts_list_row_item, null);
holder = new ViewHolder();
holder.view = (LinearLayout) convertView.findViewById(R.id.llContactRow);
holder.name = (TextView) convertView.findViewById(R.id.tvContactName);
holder.number = (TextView) convertView.findViewById(R.id.tvContactNumber);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.cbMultipleSelect);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(contacts.get(position).name);
holder.number.setText(contacts.get(position).number);
holder.view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ContactInfo contact = new ContactInfo();
contact.id = contacts.get(position).id;
contact.name = contacts.get(position).name;
contact.number = contacts.get(position).number;
holder.checkbox.setChecked(!holder.checkbox.isChecked());
if(holder.checkbox.isChecked()) {
selectedContacts.add(contact);
} else {
selectedContacts.remove(contact);
}
}
});
return convertView;
}
private static class ViewHolder {
LinearLayout view;
CheckBox checkbox;
TextView name;
TextView number;
}
}
What am I doing wrong? Please help.
EDIT:
After doing what #vinitius asked me to do the problem was resolved but the checks are disappearing when I scroll the list. Here is my new getView():
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.contacts_list_row_item, null);
holder = new ViewHolder();
holder.view = (LinearLayout) convertView.findViewById(R.id.llContactRow);
holder.name = (TextView) convertView.findViewById(R.id.tvContactName);
holder.number = (TextView) convertView.findViewById(R.id.tvContactNumber);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.cbMultipleSelect);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(contacts.get(position).name);
holder.number.setText(contacts.get(position).number);
holder.checkbox.setChecked(selectedContacts.contains(contacts.get(position)));
holder.view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ContactInfo contact = new ContactInfo();
contact.id = contacts.get(position).id;
contact.name = contacts.get(position).name;
contact.number = contacts.get(position).number;
holder.checkbox.setChecked(!holder.checkbox.isChecked());
if(holder.checkbox.isChecked()) {
selectedContacts.add(contact);
} else {
selectedContacts.remove(contact);
}
}
});
return convertView;
}
You are not showing your content in your getView(). Do the following:
#Override
public void onClick(View v) {
//remove the other stuff. You don't want to create new instances here, otherwise you won't be able to remove them later
holder.checkbox.setChecked(!holder.checkbox.isChecked());
if(holder.checkbox.isChecked()) {
selectedContacts.add(contact.get(position));
} else {
selectedContacts.remove(contact.get(position));
}
}
And do it before you set OnClickListener for your LinearLayout:
holder.checkbox.setChecked(selectedContacts.contains(contacts.get(position)));
And if you want the same behaviour when hitting the CheckBox, just add to your CheckBox xml:
android:clickable="false"
So the linearLayout can intercept it
your issue is that you need to set holder.checkbox.setChecked() to a value underneath the line:
holder.number.setText(contacts.get(position).number);
Currently you are changing the value of it when it is checked but you need to set the value when your getview method is called otherwise it will just keep whatever value it had before.
It would probably be best if you had a 'selected' value in your contactinfo rather than managing a new list. This way you could do something like:
holder.checkbox.setChecked(contacts.get(position).isSelected);
Related
I have a listview, which shows the picture, type/description of car and a checkbox. When the user now selected a car/s, I want to show them in another listview. When he unselected him from the CarListAdapter, it should also disappear from the new listview. Can I realize this with using the same Viewholder?
class CarListAdapter extends BaseAdapter
{
ArrayList<ItemsHolder> holderList;
BitmapHelper bitmapHelper = BitmapHelper.getInstance();
public CarListAdapter(ArrayList<ItemsHolder> holderList)
{
this.holderList = holderList;
}
#Override
public int getCount()
{
return holderList.size();
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(RentingDataActivity.this);
convertView = inflater.inflate(R.layout.listview_renting_entry, null, true);
holder = new ViewHolder();
holder.rowCarId = (TextView) convertView.findViewById(R.id.rowCarId);
holder.rowImageView = (ImageView) convertView
.findViewById(R.id.rowImageView);
holder.rowCarType = (TextView) convertView.findViewById(R.id.rowCarType);
holder.rowCarDescription = (TextView) convertView
.findViewById(R.id.rowCarDescription);
holder.rowCheckbox = (CheckBox) convertView.findViewById(R.id.rowCheckBox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
// holder.rowCheckbox.setOnCheckedChangeListener(null);
// holder.rowCheckbox.setTag(R.id.rowCheckBox, position);
}
// auto = holderList.get(position);
final ItemsHolder ih = holderList.get(position);
holder.rowImageView.setImageBitmap(bitmapHelper.getBigImage(ih.getImagePath(), 248));
holder.rowCarType.setText(ih.getCarType());
holder.rowCarDescription.setText(ih.getCarDescription());
// holder.rowCheckbox.setTag(ih);
holder.rowCarId.setText(ih.getCarId());
holder.rowCheckbox.setChecked(ih.isSelected());
holder.rowCheckbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox checkbox = (CheckBox)v.findViewById(R.id.rowCheckBox);
// Toast.makeText(getApplicationContext(), holder.rowCarType.getText().toString(),
// Toast.LENGTH_LONG).show();
ih.setSelected(checkbox.isChecked());
}
});
return convertView;
}
}
I would add this as a comment but alas I haven't got enough points:
add to your adapter;
notifyDataSetChanged( );
This refreshes the list asynchronously.
This question already has answers here:
Android Checkbox listview select all (disable/enable) [duplicate]
(4 answers)
Closed 8 years ago.
I have managed to implement a custom Listview which has check boxes. The user can tap a row on the listview and the checkbox will check. The positions are stored even if a user scrolls.
The listview is bound to the adapter in the onPostExecute of an Async task.
protected void onPostExecute(Boolean result) {
ImageAdapter adapter = new ImageAdapter(this, pix, paths);
lstView.setAdapter(adapter);
}
I can click on a list item and check the checkbox for that item like this:
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long row) {
CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox1);
cb.performClick();
}
I want to introduce a button which will check all the checkboxes.
I tried this but it is not working.
mSelectAllButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);//find the checkbox in the custom list
int cntChoice = mListView.getCount();
for(int i = 0; i < cntChoice; i++)
{
//if the checkbox is not clicked then click it
if(!mListView.isItemChecked(i))
{
//cb.performClick(); // didnt work
//cb.setChecked(true); // didnt work
}
}
Can anyone please explain to me clearly (I am new to Android and Java) how to change my adapter, and also how to implement this in an OnClick Listener from my main activity?
Do I have to create a new ImageAdapter for this and rebind it to the ListView?
Thanks
My Adapter:
public class ImageAdapter extends BaseAdapter {
Context context;
ArrayList<Bitmap> images;
ArrayList folderName;
boolean[] checkBoxState;
public ImageAdapter(Context c, ArrayList<Bitmap> images, ArrayList folderName){
this.context = c;
this.images = images;
this.folderName = folderName;
this.context = context;
checkBoxState=new boolean[images.size()];
imageLoader = ImageLoader.getInstance();
}
public int getCount() {
return images.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView title;
ImageView iconImage;
CheckBox checkbox;
}
public View getView(final int position, View arg1, ViewGroup arg2) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = arg1;
ViewHolder holder;
if (arg1 == null) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.title = (TextView) v.findViewById(R.id.filename);
holder.iconImage = (ImageView) v.findViewById(R.id.list_image);
holder.checkbox = (CheckBox)v.findViewById(R.id.checkBox1);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
int i = folderName.get(position).toString().lastIndexOf('\\');
if(i!= -1)
{
holder.title.setText(folderName.get(position).toString().substring(i));
}
else
{
holder.title.setText(folderName.get(position).toString());
}
holder.iconImage.setImageBitmap(images.get(position));
holder.checkbox.setChecked(checkBoxState[position]);
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked())
checkBoxState[position]=true;
else
checkBoxState[position]=false;
}
});
return v;
First you need to declare ImageAdapter adapter as global variable
Second, you used adapter in your
protected void onPostExecute(Boolean result) {
adapter = new ImageAdapter(this, pix, paths);
lstView.setAdapter(adapter);
}
Third, Add one more method in your ImageAdapter class
public void selectedAll() {
for(int i = 0; i< checkBoxState.length; i++){
checkBoxState[i] = true;
}
notifyDataSetChanged();
}
Final, call previous method in your select all click event.
if(adapter != null)
adapter.selectedAll();
First, you need to add one more parameter to your ViewHolder.
int id
Here are update for your getView() method
public View getView(final int position, View arg1, ViewGroup arg2) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = arg1;
final ViewHolder holder;
if (arg1 == null) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.title = (TextView) v.findViewById(R.id.filename);
holder.iconImage = (ImageView) v.findViewById(R.id.list_image);
holder.checkbox = (CheckBox) v.findViewById(R.id.checkBox1);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.id = position;
int i = folderName.get(position).toString().lastIndexOf('\\');
if (i != -1) {
holder.title.setText(folderName.get(position).toString()
.substring(i));
} else {
holder.title.setText(folderName.get(position).toString());
}
holder.iconImage.setImageBitmap(images.get(position));
holder.checkbox.setChecked(checkBoxState[position]);
holder.checkbox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0,
boolean isCheck) {
if (isCheck)
checkBoxState[holder.id] = true;
else
checkBoxState[holder.id] = false;
notifyDataSetChange();
}
});
return v;
}
Please let me know the result asap
Well i have a listview populated with BaseAdapter. In listview i have 2 textviews and 2 buttons that i want to be clickable and i did set it class and works fine but problem is when i click one textview, 4th, 8th list item below is clicked too and i just find out that they have same position by printing position with toast.
How can i make that positions go ++ and every of them be uniqe?
Adapter Class
public class CustomListAdapter extends BaseAdapter
{
private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
private Context mContext;
protected ListView feedListView;
public CustomListAdapter(Context context, ArrayList<FeedItem> listData)
{
this.listData = listData;
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContext = context;
}
public void addItem(final FeedItem item) {
listData.add(item);
notifyDataSetChanged();
}
#Override
public int getCount()
{
return listData.size();
}
#Override
public Object getItem(int position)
{
return listData.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
public View getView( final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
View row=convertView;
if (row == null)
{
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.name);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = (FeedItem) listData.get(position);
holder.approve.setFocusable(true);
holder.approve.setClickable(true);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
holder.approve.setTag(position);
holder.approve.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(mContext, String.valueOf(v.getTag()), Toast.LENGTH_LONG).show();
holder.approve.setTag(v);
holder.approve.setText("Approved");
}
});
return convertView;
}
static class ViewHolder
{
TextView approve;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
FeedItem newsItem;
}
}
Listview recycles views.
How ListView's recycling mechanism works
Change getView
public View getView( final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.name);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = (FeedItem) listData.get(position);
holder.approve.setFocusable(true);
holder.approve.setClickable(true);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
holder.approve.setTag(position);
holder.approve.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(mContext, String.valueOf(v.getTag()), Toast.LENGTH_LONG).show();
}
});
return convertView;
}
As you are reusing the view by
else{
holder = (ViewHolder) convertView.getTag();
}
so multiple textView using same listener. So Take your onclickListener outside of the if condition.
if (convertView == null)
{
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.name);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.approve.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(mContext, String.valueOf(position), Toast.LENGTH_LONG).show();
}
});
Add this code in your list adapter :
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return questionsClassArrayList_.size();
}
#Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
i hope anyone can help me because i'm totally lost now.
I made this custom Adapter that populates a listview from a given list.
The problem is in the minus & plus buttons. They both have listeners that modify the textview in 1 count depending on which one is pressed, an also it modifies the source list.
Here is a picture PIC that ilustrate the final view.
If the buttons pressed in first place from the first row everythings works, you can then use the rest of them whitout problem. But, if the pressed are one of the others, the application crashes.
The error given is a nullpointerexception when trying to add+1 or remove-1 to the original list on a particular list item.
If anything more is needed please ask. Thanks for your attention.
public class MenuListAdapter extends BaseAdapter {
private Context mContext;
private int mLayoutResourceId;
public MenuListAdapter(Context context, int textViewResourceId) {
mContext = context;
mLayoutResourceId = textViewResourceId;
}
public int getCount() {
return SavedMenuList.INSTANCE.size();
}
public MenuListItem getItem(int position) {
return SavedMenuList.INSTANCE.getItem(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
rowView = inflater.inflate(mLayoutResourceId, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.checkBox = (CheckBox) rowView.findViewById(R.id.confirmation_list_row_check);
viewHolder.text = (TextView) rowView.findViewById(R.id.confirmation_list_row_name);
viewHolder.minus = (ImageButton) rowView.findViewById(R.id.confirmation_list_row_remove_button);
viewHolder.minus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MenuListItem item = (MenuListItem) viewHolder.minus.getTag();
SavedMenuList.INSTANCE.removeOneItemCount(item);
notifyDataSetChanged();
}
});
viewHolder.count = (TextView) rowView.findViewById(R.id.confirmation_list_row_count);
viewHolder.plus = (ImageButton) rowView.findViewById(R.id.confirmation_list_row_add_button);
viewHolder.plus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MenuListItem item = (MenuListItem) viewHolder.plus.getTag();
SavedMenuList.INSTANCE.addOneItemCount(item);
notifyDataSetChanged();
}
});
viewHolder.itemid = (TextView) rowView.findViewById(R.id.confirmation_list_row_itemid);
viewHolder.typeid = (TextView) rowView.findViewById(R.id.confirmation_list_row_typeid);
rowView.setTag(viewHolder);
}else{
rowView = convertView;
((ViewHolder) rowView.getTag()).checkBox.setTag(getItem(position));
((ViewHolder) rowView.getTag()).minus.setTag(getItem(position));
((ViewHolder) rowView.getTag()).plus.setTag(getItem(position));
}
ViewHolder holder = (ViewHolder) rowView.getTag();
MenuListItem item = getItem(position);
holder.text.setText(item.getmItemName() + " (" + item.getmItemTypeName() + ")");
holder.count.setText(String.valueOf(item.getmItemCount()));
holder.typeid.setText(String.valueOf(item.getmItemId()));
holder.typeid.setText(String.valueOf(item.getmItemTypeId()));
return rowView;
}
static class ViewHolder {
public CheckBox checkBox;
public TextView text;
public ImageButton minus;
public TextView count;
public ImageButton plus;
public TextView itemid;
public TextView typeid;
}
}
And this is the error:
04-22 02:54:22.398: E/AndroidRuntime(7112): at giorgi.betaproject.utils.SavedMenuList.addOneItemCount(SavedMenuList.java:34)
public enum SavedMenuList {
INSTANCE;
List <MenuListItem> mList = new ArrayList<MenuListItem> ();
...
public boolean addOneItemCount(MenuListItem item) {
for (MenuListItem mItem: mList){
if (item.equals(mItem)){
mItem.addOneItemCount();
return true;
}
}
return false;
}
...
}
public boolean addOneItemCount(MenuListItem item) {
for (MenuListItem mItem: mList){
if (item.equals(mItem)){
mItem.addOneItemCount();
return true;
}
}
return false;
}
item in the above code is null, because you never set it in your getView method.
viewHolder.plus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MenuListItem item = (MenuListItem) viewHolder.plus.getTag();
SavedMenuList.INSTANCE.addOneItemCount(item);
notifyDataSetChanged();
}
});
Note this line: MenuListItem item = (MenuListItem) viewHolder.plus.getTag();, you do getTag() from the minus view, but you never do viewHolder.plus.setTag(...) in your code, which certainly causes NullPoinaterException.
I have a custom Listview each row in it consist of textview and checkbox.
(1)when I click on item in list it should go to other activity
and(2) when the checkbox is checked, the value in textview should be added to array//when it is unchecked the value should be removed from the array.
the first requirement run successfuly with me, but the second one doesn't work.
this is my code:
public class DataAdapter extends ArrayAdapter<ItemInList> {
public ArrayList<ItemInList> list;
public Activity context;
public LayoutInflater inflater;
public static ArrayList<String> array=new ArrayList<String>();
ItemInList element=new ItemInList();
public DataAdapter(Activity context,int x,ArrayList<ItemInList> list) {
super(context, 0, list);
this.context = context;
this.list = list;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
protected TextView name,Description;
protected CheckBox checkbox;
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public ItemInList getItem(int position) {
// TODO Auto-generated method stub
return list.get(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) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row1, null);
holder.name = (TextView) convertView.findViewById(R.id.food_title);
holder.name.setTextColor(Color.BLACK);
holder.Description = (TextView) convertView.findViewById(R.id.food_description);
holder.Description.setTextColor(Color.GRAY);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.add_food_item);
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
element = (ItemInList) holder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
if(element.isSelected())
{
array.add(element.getName());
}
else
{
array.remove(position);
}
}
});
convertView.setTag(holder);
} else {
holder=(ViewHolder)convertView.getTag();
ItemInList bean = (ItemInList) list.get(position);
holder.name.setText( bean.getName());
holder.Description.setText( bean.getDescription()+"");
holder.checkbox.setChecked( bean.isSelected());
return convertView;
}
holder.name.setText(list.get(position).getName());
holder.Description.setText(list.get(position).getDescription()+"");
holder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
the errors:
null pointer exception..
at DataAdapter$1.onCheckedChanged(DataAdapter.java:92)
E/AndroidRuntime(543): at android.widget.CompoundButton.setChecked(CompoundButton.java:125)
E/AndroidRuntime(543): at sehaty.com.DataAdapter$1.onCheckedChanged(DataAdapter.java:92)
E/AndroidRuntime(543): at android.widget.CompoundButton.setChecked(CompoundButton.java:125)
at android.widget.CompoundButton.performClick(CompoundButton.java:99)
any help will be appreciated
As I already stated in comments: you try to get the tag from your checkbox but you never set the tag. So element must be null. Thus you get a NullPointer. The question is..why do you try to get the tag? There is no need using that method (if i understand your intention correctly). You want to access your element variable for the corresponding postion. That element is in your list so you can try the following (the code is not tested..just copied from you and made some changes):
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row1, null);
holder.name = (TextView) convertView.findViewById(R.id.food_title);
holder.name.setTextColor(Color.BLACK);
holder.checkbox = (CheckBox) convertView
.findViewById(R.id.add_food_item);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ItemInList element = list.get(position);
holder.name.setText(element.getName());
holder.checkbox.setChecked(element.isSelected());
holder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
element.setSelected(buttonView.isChecked());
if (element.isSelected()) {
array.add(element.getName());
} else {
if (position < array.size())
array.remove(position);
}
}
});
return convertView;
}
Check line 92 in your DataAdapter class. It looks like the adapter (or other var?) is null. Have you instantiated all objects? If this doesn't help, please post your full code including XML.
In the following line
element = (ItemInList) holder.checkbox.getTag();
you are trying to get a Tag from the checkbox, which is not beng set anywhere in the getView(). As a result it returns null.