在Android列表视图中设置点击监听器按钮



我正在为一家餐馆创建一个项目订单列表。目标是将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;
        }
    }
}

注意新的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;
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新