On Clicking The Button In Listview Item, Change Occurs In Some Others Listview Items Too
I am making an app in which there are posts with comments and description. There are three buttons on each post. 1 description 2 comments and third is like button. I set a button c
Solution 1:
That's when you use an int flag for the list item's position as a conditional statement before updating the ListView's items.
So in your case, the Adapter class would look something like this:
...
privateintmPosition= -1; // Int flag that doesn't take effect UNTIL it has been set
...
@Overridepublic View getView(finalint position, View convertView, ViewGroup parent) {
a = getItem(position);
Viewview= convertView;
try {
if (convertView == null) {
v = newViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false);
v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image);
v.desc = (Button) convertView.findViewById(R.id.btn_desc);
v.des = (TextView) convertView.findViewById(R.id.tv_list_desc);
v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc);
convertView.setTag(v);
} else {
v = (ViewHolder) convertView.getTag();
}
v.desc.setTag(position);
//Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView);
Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring)
.into(v.imgView);
// Runs the following functionality if the positions match. Otherwise, hides the layout.if (mPosition == position) {
Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show();
v.des.setText(""+a.getDescription());
v.ll.setVisibility(View.VISIBLE);
} else {
v.ll.setVisibility(View.INVISIBLE);
}
v.desc.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v1) {
mPosition = position; // Sets the flag to the position that was clicked
notifyDataSetChanged(); // Updates the data instantly
}
});
return convertView;
} catch (Exception e) {
returnnull;
}
}
Let me know if that works.
Solution 2:
The best way is to implement the on click listener in adapter and set the tag on button. After that in onclick method perform the desired task through getting tag id of button, this will definitely work.
Post a Comment for "On Clicking The Button In Listview Item, Change Occurs In Some Others Listview Items Too"