简体   繁体   中英

Custom listview using button OnClickListener

i have created a custom listview with an ImageView, two TextViews and a Button now i am facing issue when i try to set onClicklistner to that button i want different method to be done for every button

here is my code for customlistview class i have used temporary onclicklistner for that button which shows the toast "Bought" what i want to do is that after clicking the button i have to return the price of the food.

class CustomListView extends ArrayAdapter {
public CustomListView(Context context, String[] resource) {
    super(context,R.layout.custom_view , resource);
}
Toast toast= null;


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater MyInflater = LayoutInflater.from(getContext());
    View CustomView = MyInflater.inflate(R.layout.custom_view, parent, false);
    String SingleItem= (String) getItem(position);
    final TextView text =(TextView)CustomView.findViewById(R.id.Itemname);
    final ImageView Image= (ImageView)CustomView.findViewById(R.id.icon);
    final TextView Pricetag= (TextView)CustomView.findViewById(R.id.PriceTextView);
    text.setText(SingleItem);
    switch (SingleItem)
    {
        case "Chicken":
            Image.setImageResource(R.drawable.desert1);
            Pricetag.setText("Rs 300");
            break;
        case "soap":
            Image.setImageResource(R.drawable.desert2);
            Pricetag.setText("Rs 300");
            break;
        case "Fish":
            Image.setImageResource(R.drawable.fish);
            Pricetag.setText("Rs 100");
            break;
        default:
            Image.setImageResource(R.drawable.myimage);
            Pricetag.setText("Rs 0.00");
            break;
    }

    final Button Buybutton= (Button)CustomView.findViewById(R.id.BuyButton);

    toast = Toast.makeText(getContext(), "", Toast.LENGTH_LONG);
    Buybutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            toast.setText("Bought");
            toast.show();

        }
    });

    text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            toast.setText(text.getText().toString());
            toast.show();

        }
    });
    return CustomView;
 }
}

For the record, there should be a better way to do this, using food-id or other approach to prevent this, but based on your request, here we go:

1- in getView() when you get a reference on the button ie:

final Button Buybutton= (Button)CustomView.findViewById(R.id.BuyButton);

do a one more step:

Buybutton.setTag(10);

10 here could be any other number, and you need to find a way to determine which number to use for each button, also it could be a string , ex value of SingleItem

Buybutton.setTag(SingleItem);

2- at onClick() you need to find out what the value assign to the view (button) and based on this value call the proper method:

@Override
public void onClick(View v) {
    if (v.getTag().toString().equals("xxxxx")){
        doSomething();
    }else if (v.getTag().toString().equals("yyyy")){
        doAnotherThing();
    }else if (v.getTag().toString().equals("zzzzz")){
        doSomething12();
    }
    //and so on...
}

this approach used String as value in setTag() and getTag() if you use integers, just replace the condition as belwo:

if (Integer.parseInt(v.getTag().toString()) == 10)

EDIT:

If i understand well, then you need to:

Buybutton.setTag(SingleItem);

and onClick():

@Override
public void onClick(View v) {
    showPriceTag(v.getTag().toString());
}

add method showPriceTag() :

public void showPriceTag(String type){
    switch (type)
        {
            case "Chicken":
                //set the price tag data ...
                break;
            case "soap":
                //set the price tag data ...
                break;
            case "Fish":
                //set the price tag data ...
                break;
            default:
                //set the default price tag data ...
                break;
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM