I have a listview with a button.When I click on the button I want the whole item to be set an alpha value and make the text of the button change. The base functionality (the item being set an alpha value and button text change) works but multiple list items are getting the same effect due to this. I have tried reading a lot posts in stackoverflow as well regarding this, no use came to me. I have really tried a lot, please help.
My code for adapter is below:
public class CustomAdapter extends BaseAdapter {
private LayoutInflater mInflater;
Context context;
ArrayList<CustomModel> rowItem;
String status = "strike";
CustomAdapter(Context context, ArrayList<CustomModel> rowItem) {
this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.rowItem = rowItem;
}
@Override
public int getCount() {
return rowItem.size();
}
@Override
public Object getItem(int position) {
return rowItem.get(position);
}
@Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
@Override
public View getView(int position, View vi, ViewGroup parent) {
//View vi = convertView; // trying to reuse a recycled view
final ViewHolder holder;
if (vi == null) {
// The view is not a recycled one: we have to inflate
vi = mInflater.inflate(R.layout.order_item, null);
holder = new ViewHolder();
holder.rm = rowItem.get(position);
holder.rootLayout = (LinearLayout) vi.findViewById(R.id.root_Layout);
holder.tv_name = (TextView) vi.findViewById(R.id.item_name);
holder.tv_quantity = (TextView) vi.findViewById(R.id.item_qty);
holder.tv_price = (TextView) vi.findViewById(R.id.item_price);
holder.tv_rate = (TextView) vi.findViewById(R.id.item_rate);
holder.layout1 = (LinearLayout) vi.findViewById(R.id.layout1);
holder.layout2 = (LinearLayout) vi.findViewById(R.id.layout2);
holder.layout3 = (LinearLayout) vi.findViewById(R.id.layout3);
holder.layout4 = (LinearLayout) vi.findViewById(R.id.layout4);
holder.tv_prefs = (TextView) vi.findViewById(R.id.pref_name);
holder.baseLayout = (LinearLayout) vi
.findViewById(R.id.base_layout);
holder.strikeoff = (Button) vi.findViewById(R.id.item_strike);
holder.status = "strike";
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
//holder.strikeoff.setTag(holder);
holder.strikeoff.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//@SuppressWarnings("unused")
//ViewHolder holder = (ViewHolder) v.getTag();
if (holder.status.equals("strike")) {
holder.tv_rate.setPaintFlags(holder.tv_rate.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.tv_price.setPaintFlags(holder.tv_rate.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.layout1.setAlpha(.25f);
holder.layout2.setAlpha(.25f);
holder.layout3.setAlpha(.25f);
holder.layout4.setAlpha(.25f);
holder.tv_name.setAlpha(.25f);
holder.strikeoff.setText("Undo");
holder.status = "undo";
}
}
});
if (rowItem != null || !rowItem.isEmpty() || rowItem.size() != 0) {
holder.tv_name.setText(rowItem.get(position).getItemName());
holder.tv_quantity.setText(rowItem.get(position).getItemQuantity());
holder.tv_price.setText("Price: "
+ rowItem.get(position).getItemPrice());
holder.tv_rate.setText("Rate :"
+ rowItem.get(position).getItemRate());
if (!rowItem.get(position).getPrefName().equals("Nil")) {
holder.tv_prefs.setText(rowItem.get(position).getPrefName());
}
// Alternate colour for rows in listview
if (position % 2 == 0) {
holder.baseLayout.setBackgroundColor(Color.WHITE);
} else {
holder.baseLayout.setBackgroundColor(Color
.parseColor("#ededed"));
}
}
return vi;
}
class strikeButtonClickListener implements OnClickListener {
int position;
ViewHolder holder;
public strikeButtonClickListener(int pos, ViewHolder holder) {
this.position = pos;
this.holder = holder;
}
public void onClick(View v) {
if (holder.status.equals("strike")) {
holder.tv_rate.setPaintFlags(holder.tv_rate.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.tv_price.setPaintFlags(holder.tv_rate.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.layout1.setAlpha(.25f);
holder.layout2.setAlpha(.25f);
holder.layout3.setAlpha(.25f);
holder.layout4.setAlpha(.25f);
holder.tv_name.setAlpha(.25f);
holder.strikeoff.setText("Undo");
holder.status = "undo";
} else {
holder.tv_rate.setPaintFlags(holder.tv_rate.getPaintFlags()
& (~Paint.STRIKE_THRU_TEXT_FLAG));
holder.tv_price.setPaintFlags(holder.tv_rate.getPaintFlags()
& (~Paint.STRIKE_THRU_TEXT_FLAG));
holder.layout1.setAlpha(1f);
holder.layout2.setAlpha(1f);
holder.layout3.setAlpha(1f);
holder.layout4.setAlpha(1f);
holder.tv_name.setAlpha(1f);
holder.strikeoff.setText("Strike off");
holder.status = "strike";
}
}
}
static class ViewHolder {
CustomModel rm;
Button strikeoff;
View strikeview;
TextView tv_name, tv_quantity, tv_price, tv_rate, tv_prefs, ratetxt,
pricetxt;
LinearLayout baseLayout, rootLayout, layout1, layout2, layout3,
layout4;
FrameLayout frameLayout;
ViewGroup vg;
String status = "strike";
}
}
Try to add one String status flag on
CustomModel
class and set deafult value to this flag :Now try to change status value vice-verse when click occurs and notify
adapter
:Change list item alpha and text value base on status in
getView()
: