sharath
sharath

Reputation: 521

button click inside listview item mixing up action on items

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";
}

}

Upvotes: 2

Views: 967

Answers (2)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Try to add one String status flag on CustomModel class and set deafult value to this flag :

public class CustomModel {
    private String status = "strike";

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

Now try to change status value vice-verse when click occurs and notify adapter:

holder.strikeoff.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
       if(rowItem.get(position).getStatus().equals("strike")){
         rowItem.get(position).setStatus("undo");
       }else{
         rowItem.get(position).setStatus("strike");
       }
       notifyDataSetChanged();
    }
}

Change list item alpha and text value base on status in getView() :

@Override
public View getView(int position, View vi, ViewGroup parent) {

       if(rowItem.get(position).getStatus().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");
        } 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");
        }


        return vi;
    }

Upvotes: 2

KDOSHI
KDOSHI

Reputation: 317

Use SetTag getTag like...

    public class Audio_Post_Adapter extends BaseAdapter {
Context mContext;
ArrayList<String> mSampalList;
Holder mHolder;
LayoutInflater inflater;

public Audio_Post_Adapter(Context mContext) {
    // TODO Auto-generated constructor stub
    inflater = LayoutInflater.from(mContext);
    this.mContext = mContext;
    this.mSampalList = mSampalList;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 5;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return mSampalList.get(arg0);
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertview, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view = convertview;
    if(view == null){

        view = LayoutInflater.from(mContext).inflate(
                R.layout.view_audio, null,false);
        mHolder = new Holder();

        mHolder.mLn_main_audio = (LinearLayout) view.findViewById(R.id.mLn_main_audio);
        mHolder.mImg_profile_user =  (CustomNetworkImageView) view.findViewById(R.id.mImg_profile_user);

        view.setTag(mHolder);

    }else{

        mHolder = (Holder) view.getTag();
    }
    mHolder.mImg_profile_user.setTag(position);
    mHolder.mImg_profile_user.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int index = (Integer) v.getTag();
            //What ever list is take the data from it using this index
            String data = mSampalList.get(index);
        }
    });
    return view;
}

public class Holder{
    private LinearLayout mLn_main_audio;
    private CustomNetworkImageView mImg_profile_user,mImg_post_img;

}


   }

Upvotes: 0

Related Questions