Im using a array adapter with a so show a list of linear layouts. The layouts have 3 text fields and a hidden button that is shown when the list item is clicked. What I am trying to do is attach a onclicklistener to the button that will delete that row from the arraylist/adapter and update the data set. I have the following code:
public class CheckAdapter extends ArrayAdapter<Check> {
Context context;
int layoutResourceId;
ArrayList<Check> data = null;
public CheckAdapter(Context context, int resource, ArrayList<Check> objects) {
super(context, resource, objects);
this.context = context;
this.layoutResourceId = resource;
this.data = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder(convertView.findViewById(R.id.checklistRoot));
holder.checkName = (TextView) convertView.findViewById(R.id.checklistname);
holder.checkNumber = (TextView) convertView.findViewById(R.id.checklistnumber);
holder.checkTotal = (TextView) convertView.findViewById(R.id.checklisttotal);
holder.deleteButton = (Button) convertView.findViewById(R.id.deletecheck);
holder.swipeButtons();
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
holder.swipeButtons();
}
String name = data.get(position).getName();
Double total = data.get(position).getAmmount();
Long number = data.get(position).getCheckNumber();
holder.checkName.setText(name);
holder.checkNumber.setText(number.toString());
holder.checkTotal.setText(total.toString());
holder.deleteButton.setTag(position);
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer position = (Integer) view.getTag();
Log.d("Check Adapter", "Button Clicked: " + position);
data.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
static class ViewHolder{
TextView checkName;
TextView checkNumber;
TextView checkTotal;
Button deleteButton;
View rootView;
ViewHolder(View rootView) {
this.rootView = rootView;
}
public void swipeButtons(){
rootView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
deleteButton.setVisibility(View.VISIBLE);
return false;
}
});
}
}
}
My logs are showing I have the correct position being sent into data.remove(position); However the ListView doesn't seem to be updating and the data seems to be remaining. Any suggestions?
Thanks so much!
You can also try using position value provided by getView method itself.
Also remove line holder.deleteButton.setTag(position); from getView method.
so your code will be like below :
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// --- Your other code ---//
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
Hope this will help you.
Related
private class MyAdapter extends ArrayAdapter {
public MyAdapter(Context context, int resource, ArrayList objects) {
super(context, resource, objects);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = getLayoutInflater().inflate(R.layout.item,null);
ImageButton btn_cancel = v.findViewById(R.id.btn_cancelDownload);
btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();
}
});
return v;
}
}
I am using custom listview and in this Imagebutton on click listener not working i also tried on listview.setOnItemSelectedListener that is also not working i also changed the layout. But this is not working.
Please try to create a adapter like this.
public class CustomAdapter extends ArrayAdapter<DataModel> implements View.OnClickListener{
private ArrayList<DataModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView txtName;
TextView txtType;
TextView txtVersion;
ImageView info;
}
public CustomAdapter(ArrayList<DataModel> data, Context context) {
super(context, R.layout.row_item, data);
this.dataSet = data;
this.mContext=context;
}
#Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
DataModel dataModel=(DataModel)object;
switch (v.getId())
{
case R.id.item_info:
Snackbar.make(v, "Release date " +dataModel.getFeature(), Snackbar.LENGTH_LONG)
.setAction("No action", null).show();
break;
}
}
private int lastPosition = -1;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
DataModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);
viewHolder.txtType = (TextView) convertView.findViewById(R.id.type);
viewHolder.txtVersion = (TextView) convertView.findViewById(R.id.version_number);
viewHolder.info = (ImageView) convertView.findViewById(R.id.item_info);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
result.startAnimation(animation);
lastPosition = position;
viewHolder.txtName.setText(dataModel.getName());
viewHolder.txtType.setText(dataModel.getType());
viewHolder.txtVersion.setText(dataModel.getVersion_number());
viewHolder.info.setOnClickListener(this);
viewHolder.info.setTag(position);
// Return the completed view to render on screen
return convertView;
}
}
You can create adpater like this, please tell me if this can help you, i am sharing reference link also.
https://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial
In last week, i just try to resolve this problem. see every similar questions and i don't know why, solutions don't work for me.
I have a list view and each item has an image view. when clicked on image view , i want to change image resource. well. all done. but when scrolling, image resources change.
my full code is:
public class CustomBaseAdapter extends BaseAdapter {
Context context;
ArrayList<String> items;
public CustomBaseAdapter(Context context,ArrayList<String> items){
this.context = context;
this.items = items;
}
private class ViewHolder{
TextView titr;
ImageView image;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return items.hashCode();
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
final ViewHolder holder ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view==null){
vi = inflater.inflate(R.layout.customlistitem,null);
holder = new ViewHolder();
holder.titr = (TextView) vi.findViewById(R.id.listtext);
holder.image = (ImageView) vi.findViewById(R.id.listimg);
holder.image.setTag(position);
vi.setTag(holder);
}
else{
holder = (ViewHolder) vi.getTag();
}
holder.titr.setText(items.get(position));
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.fav_ic);
}
});
return vi;
}
}
where i have mistake?
you have to use one boolean value in holder to check whether your background image is set or not for particular row.
holder
private class ViewHolder{
TextView titr;
ImageView image;
boolean isSet;
}
set image
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.fav_ic);
holder.isSet = true
}
});
if(holder.isSet)
{
holder.image.setBackgroundResource(R.drawable.fav_ic);
}
else{
holder.image.setBackgroundResource(no image or other image);
}
hope this will help.
try this,
public class CustomBaseAdapter extends BaseAdapter {
Context context;
ArrayList<Items> items;
public CustomBaseAdapter(Context context,ArrayList<Items> items){
this.context = context;
this.items = items;
}
private class ViewHolder{
TextView titr;
ImageView image;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Items getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return items.hashCode();
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
final ViewHolder holder ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view==null){
vi = inflater.inflate(R.layout.customlistitem,null);
holder = new ViewHolder();
holder.titr = (TextView) vi.findViewById(R.id.listtext);
holder.image = (ImageView) vi.findViewById(R.id.listimg);
holder.image.setTag(position);
vi.setTag(holder);
}
else{
holder = (ViewHolder) vi.getTag();
}
holder.titr.setText(items.get(position).getStrTxt());
holder.image.setImageResource(items.get(position).getDrawableImage());
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
items.set(position,new Items(R.drawable.fav_ic));
notifyDataSetChanged();
}
});
return vi;
}
}
Add Model Class :
public class Items {
int drawableImage;
String strTxt;
public Items(int drawableImage) {
this.drawableImage = drawableImage;
}
public int getDrawableImage() {
return drawableImage;
}
public void setDrawableImage(int drawableImage) {
this.drawableImage = drawableImage;
}
public String getStrTxt() {
return strTxt;
}
public void setStrTxt(String strTxt) {
this.strTxt = strTxt;
}
}
This is because when you scroll listview android system always create new row and destroy non-visible rows. You need to modify the getView() as below then everything will be fine :
#Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
final ViewHolder holder ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(view==null){
vi = inflater.inflate(R.layout.customlistitem,null);
holder = new ViewHolder();
holder.titr = (TextView) vi.findViewById(R.id.listtext);
holder.image = (ImageView) vi.findViewById(R.id.listimg);
holder.image.setTag(position);
vi.setTag(holder);
}
else{
holder = (ViewHolder) vi.getTag();
}
holder.titr.setText(items.get(position));
if(sp.getBoolean("position="+position, false)){
v.setBackgroundResource(R.drawable.fav_ic);
}
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.fav_ic);
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean("position="+position, true);
edit.commit();
}
});
return vi;
}
You should define layoutInflater in the constructor because getView() call for each row so if you define layoutInflater in getView() then LayoutInflater define again and again for each row.
Yes, this problem appears always in ListView because android always destroy and recreate rows in it.
You have two choices:
First, Modify your Adapter to save status for each row in the ListView and check this status before displaying the row.
Second one, I recommended this one and use it in my code, you can use RecyclerView, this problem will not happen with it.
I have a ListView that contains 5 rows with 5 button:
When I click on first button, I replace it text with "B".
Then, I click on second button, I replace it text with "B", but how can I replace the previous clicked button (the first) with "A"?
This is what really happen:
This is my code for the button click:
private int resource;
private LayoutInflater inflater;
ViewHolder holder;
private Context ctx;
public RingtoneAdapter(Context context, int resourceId, List<Ringtone> objects) {
super(context, resourceId, objects);
this.ctx = context;
resource = resourceId;
inflater = LayoutInflater.from(context);
}
public View getView(final int position, View v, ViewGroup parent) {
// Recuperiamo l'oggetti che dobbiamo inserire a questa posizione
final Ringtone ringtone = getItem(position);
if (v == null) {
v = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
holder.btnPlay = (Button) v.findViewById(R.id.btnPlay);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button btnadd = (Button) view;
btnadd.setText("B");
notifyDataSetChanged();
}
});
return v;
}
private static class ViewHolder {
Button btnPlay;
TextView txtTitolo;
TextView txtCategoria;
}
If I'm reading this correctly, you only want a single position to be 'selected' at a time. If that's the case, you could keep a reference to the selected position, and use the Adapter's functionality instead of having multiple buttons know about each other. e.g.
private int selectedPosition = -1;
#Override
public View getView(final int position, View convertView, ViewGroup parent){
// ... Your ViewHolder / Ringtone initialization
if(position == selectedPosition)
holder.btnPlay.setText("B");
else holder.btnPlay.setText("A");
holder.btnPlay.setOnClickListener(new View.OnClickListener(){
if(holder.btnPlay.getText().toString().equals("A"))
selectedPosition = position;
else selectedPosition = -1;
notifyDataSetChanged();
});
return convertView;
}
The key here is allowing notifyDataSetChanged() to internally call your getView method, which then really only needs to know how to render the data in its current state, instead of dealing with the complex logic required to map between multiple Views.
you need to reset the items which were not clicked,
btn.setText("B"); // if the item was clicked
btn.setText("A"); // for all other items
what you can do is, cycle through all elements setting the text for button like this btn.setText("A"), once this is done call btn.setText("B")
you should be using OnItemClickListener on the listview, it is easy that way, as you are setting the value in view holder, it does not give you access to the other items in the listview, but by using OnItemClickListener you can cover all the elements in the listview once you update things as you like call notifyDataSetChanged(), As this can be done there is another way to look at the problem
ListView listView = (ListView) findViewById(R.id.ListViewTestSevenActivity_listView);
ArrayList<Item> items = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Item item = new Item("A");
items.add(item);
}
MyAdapter adapter = new MyAdapter(getApplicationContext(), R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
This will take care of initialization and rest of the code as you want it to work. code is also available on github
private class MyAdapter extends ArrayAdapter {
private Context a_context;
private ArrayList<Item> items;
private LayoutInflater a_layoutInflater;
public MyAdapter(Context context, int resource, ArrayList<Item> items) {
super(context, resource, items);
this.a_context = context;
this.items = items;
a_layoutInflater = LayoutInflater.from(this.a_context);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
row = a_layoutInflater.inflate(R.layout.single_item_listview_seven, parent, false);
holder = new ViewHolder();
holder.button = (Button) row.findViewById(R.id.ListViewTestSevenActivity_text_button);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Item item = items.get(position);
System.out.println("" + item.getText());
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (Item current_item : items) {
current_item.setText("A");
}
item.setText("B");
notifyDataSetChanged();
}
});
holder.button.setText("" + item.getText());
return row;
}
private class ViewHolder {
Button button;
}
}
private class Item {
String text;
public Item(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
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 know that this question has been asked over and over again but still I've not been a able to find a suggestion that really helps me. The checkbox is unchecked whenever the list is scrolled down. Yes I'm using a boolean array to store the values but this still doesn't fix the problem. Here is my code. Please suggest a solution for this. Thank you.
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
final boolean[] itemChecked=new boolean[30];
LayoutInflater inflater = context.getLayoutInflater();
if(convertView==null)
{
convertView = inflater.inflate(R.layout.custom_list, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.description_text);
holder.cb=(CheckBox) convertView.findViewById(R.id.cb);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
itemChecked[position] = isChecked;
if(itemChecked[position])
{
holder.cb.setChecked(true);
}
else
{
holder.cb.setChecked(false);
}
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
holder.cb.setChecked(itemChecked[position]);
holder.txtViewDescription.setFocusable(false);
holder.txtViewTitle.setFocusable(false);
return convertView;
}
}
getView() is called whenever a previously invisible list item needs to be drawn. Since you recreate itemChecked[] each time this method is called you will have the new checkbox unchecked and a different Array for each resulting View. (final in Java does not make that field unique like in C)
Simplest way to solve that is to make itemChecked a classmember and set / restore checkbox state based on that one.
public class MyListAdapter extends ArrayAdapter<Object> {
private final boolean[] mCheckedState;
private final Context mContext;
public MyListAdapter(Context context, int resource, int textViewResourceId, List<Object> objects) {
super(context, resource, textViewResourceId, objects);
mCheckedState = new boolean[objects.size()];
mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// simplified to just a Checkbox
// ViewHolder and OnCheckedChangeListener stuff left out
CheckBox result = (CheckBox)convertView;
if (result == null) {
result = new CheckBox(mContext);
}
result.setChecked(mCheckedState[position]);
return result;
}
}
Here is an example. Read the comments in the getView(...)
of the adapter provided below.
class TaskObject {
private int pid;
private String processName;
private boolean toKill;
///getters/setters
public boolean isToKill() {
return toKill;
}
public void setToKill(boolean toKill) {
this.toKill = toKill;
}
................................
}
class TaskListAdapter extends BaseAdapter {
private static final String TAG = "adapter";
ArrayList<TaskObject> list;
Context context;
public TaskListAdapter(Context context) {
Log.d(TAG, "created new task list adapter");
this.context = context;
if (list == null) {
list = new ArrayList<TaskObject>();
}
}
public void addTask(TaskObject taskObject) {
list.add(taskObject);
}
public void clearTasks() {
list.clear();
Log.d(TAG, "list size:" + list.size());
this.notifyDataSetChanged();
}
public int getCount() {
return list.size();
}
public TaskObject getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
RelativeLayout rl = new RelativeLayout(context);
TextView textPid = new TextView(context);
textPid.setText(Integer.toString(getItem(position).getPid()));
TextView textName = new TextView(context);
textName.setText(getItem(position).getProcessName());
/////Here is your and it will set back your checked item after scroll
CheckBox chckKill = new CheckBox(context);
if(getItem(position).isToKill())
{
chckKill.setChecked(true);
}
////////////////////////////////////////////////////////////////////
chckKill.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
getItem(position).setToKill(true);
}
}
});
chckKill.setTag(getItem(position).getPid());
/////////NOT LAYOUTED USE LAYOUT
rl.addView(chckKill);
rl.addView(textName);
rl.addView(textPid);
return rl;
}
hope it helps abit.
In mycase, I solved this issue as follows :
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
ImageView thumbnail = null;
CheckBox checkBox = null;
Content rowData = GridViewActivity.contents.get(position);
if (null == convertView) {
convertView = mInflater.inflate(R.layout.grid_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.getContentTitle();
title.setText(rowData.getTitle());
thumbnail = holder.getThumbnail();
thumbnail.setImageResource(rowData.getIcon());
checkBox = holder.getCheckBox();
checkBox.setTag(position);
checkBox.setChecked(rowData.isCheckBox());
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
GridViewActivity.notifyCheckChanges(getPosition,
buttonView.isChecked());
}
});
return convertView;
}
You should place setChecked() after setOnCheckedChangeListener().