使用按钮 OnClickListener 的自定义列表视图

Custom listview using button OnClickListener

我创建了一个带有一个 ImageView、两个 TextView 和一个 Button 的自定义列表视图 现在我在尝试将 onClicklistner 设置为该按钮时遇到问题 我想对每个按钮使用不同的方法

这是我的自定义列表视图代码 class 我为那个显示吐司的按钮使用了临时的 onclicklistner "Bought" 我想要做的是在点击按钮后我必须 return 食物的价格。

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

郑重声明, 应该有更好的方法来做到这一点,使用 food-id 或其他方法来防止这种情况,但根据您的要求,我们开始:

1- in getView() 当您在按钮上获得参考时,即:

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

再做一步:

Buybutton.setTag(10);

10 可以是任何其他数字,您需要找到一种方法来确定每个按钮使用哪个数字,也可以是字符串,例如 SingleItem

的值
Buybutton.setTag(SingleItem);

2-onClick() 您需要找出分配给视图(按钮)的值,并根据此值调用正确的方法:

@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...
}

此方法使用字符串作为 setTag()getTag() 中的值 如果您使用整数,只需将条件替换为 belwo:

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

编辑:

如果我理解的很好,那么你需要:

Buybutton.setTag(SingleItem);

和 onClick():

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

添加方法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;
        }
}