2

ボタンと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イベントの設定にご協力ください

4

2 に答える 2

6

リスナーを追加するだけです。これはListViewmyButton.setOnClickListener(mBuyButtonClickListener) で行われます。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
    }
}

};

さらにヘルプが必要な場合は、この記事にアクセスしてください..回答がお役に立てば幸いです。

于 2012-12-17T14:10:34.480 に答える
1

完成したコードは、単純すぎる以下にリストされています。

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, btn_delete;
    TextView item_name;
    TextView item_price;
    TextView item_total;
    EditText et_quantity;

}

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

    final 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.orderlist_plus);
        holder.btn_minus = (Button) convertView.findViewById(R.id.orderlist_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);

        holder.btn_delete = (Button) convertView.findViewById(R.id.btn_order_list_delete);

        convertView.setTag(holder);

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

    final 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));

    holder.btn_delete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            list.remove(position);
            notifyDataSetChanged();
            //Log.v("sd", "" + alist);

        }
    });

    holder.btn_minus.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            //to get a specific item
            Toast.makeText(activity,"" + holder.item_name.getText().toString(),Toast.LENGTH_LONG).show();

        }
    });

    /*
     * holder.et_quantity.addTextChangedListener(new TextWatcher() {
     * 
     * @Override public void onTextChanged(CharSequence arg0, int arg1, int
     * arg2, int arg3) { // TODO Auto-generated method stub
     * 
     * }
     * 
     * @Override public void beforeTextChanged(CharSequence arg0, int arg1,
     * int arg2, int arg3) { // TODO Auto-generated method stub
     * 
     * }
     * 
     * @Override public void afterTextChanged(Editable arg0) { // TODO
     * Auto-generated method stub Toast.makeText(activity,
     * ""+getItem(position), 1000).show(); } });
     */
    return convertView;
}

}
于 2012-12-20T10:06:05.223 に答える