问题描述
我有一个按钮和一个TextView一个ListView。如何设置onclicklistener为ListView里面的按钮?
code如下:
I have a listView having a button and a textView. How can I set onclicklistener for the button inside the listView?Code Given below:
ListView.xml:
ListView.xml:
ListView lv1 = (ListView)popupView.findViewById(R.id.listView1);
listviewAdapter adapter = new listviewAdapter(Order_page.this, alist);
lv1.setAdapter(adapter);
ListviewAdapter类如下:
ListviewAdapter Class is as follows:
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事件
Please help to set onClick event of the button
推荐答案
您只需要添加一个监听器与 myButton.setOnClickListener(mBuyButtonClickListener)
这将在 getView()来完成
你的ListView
You just have to add a listener with myButton.setOnClickListener(mBuyButtonClickListener)
This will be done in getView()
of you ListView
您可以通过了解按钮的位置 myListView.getPositionForView(myButton的)
下面的解决方案可以帮助您:
you can know position of the button by using myListView.getPositionForView(myButton)
Below solution can help you:
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
}
}
};
有关进一步的帮助,你可以去。希望回答可以帮助你。
For further help you can go to this article .. Hope answer helps you..
这篇关于如何为android系统的列表视图内按钮onclicklistener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!