设置单击侦听器到列表视图Android中的按钮

时间:2015-07-08 23:18:36

标签: android listview button row onclicklistener

我正在为restaurrant创建商品订单清单。 目标是将onClickListener设置为列表中每行中的每个按钮(+/-),以便计算订购的项目数。 有谁能帮我这个代码?我设法设置了一个监听器,但是在行中点击时它没有正确计算项目。

public class MainActivity extends ListActivity {

    private String[] item_names = {"Mozza Cheese", "Chicken Rings", "Onion Rings", "Calamari Rings"};
    private int item_counter = 1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.view_order_list);

        listAdapter();

    }

    private void listAdapter(){
        setListAdapter(new MyAdapter(this,
                android.R.layout.simple_list_item_2, R.id.items_name_order, item_names));

    }

    private class MyAdapter extends ArrayAdapter<String> {

        public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
            super(context, resource, textViewResourceId, strings);

        }
        @SuppressLint("ViewHolder")
        public View getView(final int position, View convertView, ViewGroup parent) {


            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.view_order, parent, false);

            TextView item_name = (TextView) convertView.findViewById(R.id.items_name_order);
            final TextView item_count = (TextView) convertView.findViewById(R.id.count_item_order);

            Button plus = (Button) convertView.findViewById(R.id.button_plus_order);
            Button minus = (Button) convertView.findViewById(R.id.button_minus_order);

            item_name.setText(item_names[position]);

            plus.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    item_counter++;
                    item_count.setText(Integer.toString(item_counter));

                }
            });


            return convertView;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

注意新的int数组item_counter,请在我的问题评论

中休息
public class MainActivity extends ListActivity {

    private String[] item_names = {"Mozza Cheese", "Chicken Rings", "Onion Rings", "Calamari Rings"};
    private int[] item_counter = {0, 0, 0, 0};

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.view_order_list);

        listAdapter();

    }

    private void listAdapter(){
        setListAdapter(new MyAdapter(this,
                android.R.layout.simple_list_item_2, R.id.items_name_order, item_names));

    }

    private class MyAdapter extends ArrayAdapter<String> {

        public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
            super(context, resource, textViewResourceId, strings);

        }
        @SuppressLint("ViewHolder")
        public View getView(final int position, View convertView, ViewGroup parent) {


            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.view_order, parent, false);

            TextView item_name = (TextView) convertView.findViewById(R.id.items_name_order);
            final TextView item_count = (TextView) convertView.findViewById(R.id.count_item_order);

            Button plus = (Button) convertView.findViewById(R.id.button_plus_order);
            Button minus = (Button) convertView.findViewById(R.id.button_minus_order);

            item_name.setText(item_names[position]);
            item_count.setText(item_counter[position];
            plus.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    item_counter[position]++;
                    notifyDataSetChanged();

                }
            });


            return convertView;
        }
    }
}