在列表视图中查找单击的行并获取数据

时间:2013-05-24 01:47:59

标签: android listview buttonclick custom-adapter

我正在使用自定义适配器来显示我的ArrayList中的项目。每个行项目都有2个textViews和一个图像按钮。根据属性的状态,图像按钮上的图像正在变化。虽然看起来很简单,但是当单击图像按钮时,我找不到从行项(在我的情况下为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;
}

1 个答案:

答案 0 :(得分:4)

将位置设置为deleteButton标记

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

然后点击

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