gpt4 book ai didi

android-layout - 如何在 android 的 ListView 中为按钮设置 onclicklistener?

转载 作者:行者123 更新时间:2023-12-01 09:29:41 24 4
gpt4 key购买 nike

我有一个带有按钮和 TextView 的 ListView 。如何为 listView 中的按钮设置 onclicklistener?代码如下:

ListView .xml:

ListView lv1 = (ListView)popupView.findViewById(R.id.listView1);
listviewAdapter adapter = new listviewAdapter(Order_page.this, alist);
lv1.setAdapter(adapter);

ListviewAdapter类如下:

public class listviewAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> list;
Activity activity;

public listviewAdapter(Activity activity,
ArrayList<HashMap<String, String>> list) {
super();
this.activity = activity;
this.list = list;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

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

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

class ViewHolder {
Button btn_fav, btn_plus, btn_minus;
TextView item_name;
TextView item_price;
TextView item_total;
EditText et_quantity;
}

@Override
public View getView(int position, View convertView, final ViewGroup parent) {
// TODO Auto-generated method stub

// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();

if (convertView == null) {

convertView = inflater.inflate(R.layout.order_list_texts, null);
holder = new ViewHolder();

holder.btn_fav = (Button) convertView.findViewById(R.id.fav_btn_orderlist);
holder.btn_plus = (Button) convertView.findViewById(R.id.order_pg_plus);
holder.btn_minus = (Button) convertView.findViewById(R.id.order_pg_minus);

holder.item_name = (TextView) convertView.findViewById(R.id.item_name);
holder.item_price = (TextView) convertView.findViewById(R.id.itm_price);
holder.item_total = (TextView) convertView.findViewById(R.id.Item_total);
holder.et_quantity = (EditText) convertView.findViewById(R.id.et_quantity_order_list);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

HashMap<String, String> map = list.get(position);
holder.item_name.setText(map.get(ITEM_NAME_COLUMN));
holder.item_price.setText(map.get(ITEM_PRICE_COLUMN));
holder.item_total.setText(map.get(TOTAL_COLUMN));
holder.et_quantity.setText(map.get(ITEM_QUANTITY_COLUMN));

return convertView;
}

}

请帮忙设置按钮的onClick事件

最佳答案

您只需要使用 myButton.setOnClickListener(mBuyButtonClickListener) 添加一个监听器这将在您的 ListView 的 getView() 中完成

您可以使用 myListView.getPositionForView(myButton) 知道按钮的位置以下解决方案可以帮助您:

private OnClickListener mBuyButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
final int position = getListView().getPositionForView(v);
if (position != ListView.INVALID_POSITION) {
//DO THE STUFF YOU WANT TO DO WITH THE position
}
}

};

如需进一步帮助,您可以转到 this article ..希望回答对你有帮助..

关于android-layout - 如何在 android 的 ListView 中为按钮设置 onclicklistener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13915559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com