程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> adapter-在listview中找到點擊的行,獲取數據

adapter-在listview中找到點擊的行,獲取數據

編輯:編程綜合問答
在listview中找到點擊的行,獲取數據

我使用自定義的 adapter在ArrayList中顯示items。每一行item都2個 textViews和1個按鈕。
依據一個屬性的狀態,在圖像按鈕上的圖像會改變。當點擊image button時,如何從行的item(orderID )中或取信息?

public class OrderedAdapter extends ArrayAdapter<RMOrder> {

private Context context;
private RMOrder rowItem;
private ListView lv;
private int clickedID;
private ArrayList<RMOrder>items;
private int orderID;
private ViewHolder holder;

public OrderedAdapter(Context context, int textViewResourceId, List<RMOrder> objects) {
    super(context, textViewResourceId, objects);
    this.context = context;
}

/*private view holder class*/
private class ViewHolder {
    ImageButton deleteButton;
    TextView txtMenuName;
    TextView txtMenuDesc;
    TextView txtMenuPrice;
    int orderID;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    holder = null;
    rowItem = getItem(position);
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.basket_list_row, null);
        holder = new ViewHolder();
        holder.txtMenuName = (TextView) convertView.findViewById(R.id.menuNameText);
        holder.txtMenuDesc = (TextView) convertView.findViewById(R.id.menuDetailText);
        holder.deleteButton = (ImageButton) convertView.findViewById(R.id.menuDelete);
        holder.txtMenuPrice = (TextView) convertView.findViewById(R.id.menuPrice);
        holder.orderID = 0;
        lv = (ListView) convertView.findViewById(R.id.orderedList);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    String detailsString = "";
    double price = rowItem.getPrice();

    for (RMOption i : rowItem.getOption()) {
        detailsString += "-" + i.getName();
        price += i.getExtraPrice();
    }

    holder.orderID = rowItem.getId();
    holder.txtMenuName.setText(rowItem.getMenuName());
    holder.txtMenuDesc.setText(detailsString);
    holder.txtMenuPrice.setText(Double.toString(price) + " TL");
    holder.deleteButton.setFocusable(false);

    items = new ArrayList<RMOrder>(GlobalList.orderedItemsList.values());
    clickedID = items.get(position).getId();
    orderID = holder.orderID;
    //change image of image button
    if(rowItem.getStatus().equals("pending")){
        holder.deleteButton.setImageResource(R.drawable.rsz_pending);
        holder.deleteButton.setOnClickListener(new OnClickListener() {
            //TODO Burada tıklananı bulmakta sıkıntı var
            public void onClick(View v) {

                       //GET ORDER ID
            }
       });
    else
        holder.deleteButton.setImageResource(R.drawable.rsz_approved);
    }
    return convertView;
}

最佳回答:


給 deleteButton tag 設置位置

holder.deleteButton = (ImageButton) convertView.findViewById(R.id.menuDelete);
holder.deleteButton.setTag(position);   

然後設置 onClick 方法

holder.deleteButton.setOnClickListener(new OnClickListener() {
        //TODO Burada tıklananı bulmakta sıkıntı var
        public void onClick(View v) {
                  int pos = Integer.parseInteger(v.getTag());
                  RMOrder rItem = getItem(pos);
                  int orderId = rowItem.getId();
        }
   });
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved