android studio 中点击button加1或者减1

我们这次的项目,需要这个功能,点击“+”就加1,点击“-”就减1,用来确定菜单的数量

主要java代码如下
参考于http://www.2cto.com/kf/201608/539914.html这个不是在android studio 中的,但同理。
。。。

public class MainActivity extends Activity {
private List<HashMap<String, String>> mList = null;
private ListView mListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	mListView = (ListView) this.findViewById(R.id.mList);
	mList = new ArrayList<HashMap<String, String>>();
	Random rdom = new Random();
	for (int i = 0; i < 30; i++) {
		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("serial", i + "");
		hashMap.put("content", "����-" + rdom.nextInt(10000));
		hashMap.put("number", rdom.nextInt(20) + "");
		mList.add(hashMap);
	}
	mListView.setAdapter(new MAdapter());
}

private class MAdapter extends BaseAdapter {
	private ViewHolder holder = null;

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView,
			ViewGroup parent) {
		AbsListView.LayoutParams lParams = new AbsListView.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT, 100);
		if (null == convertView) {
			holder = new ViewHolder();
			convertView = View.inflate(getApplicationContext(),
					R.layout.activity_main_item, null);

			holder.item_content = (TextView) convertView
					.findViewById(R.id.item_content);
			holder.item_reduce = (Button) convertView
					.findViewById(R.id.item_reduce);
			holder.item_number = (TextView) convertView
					.findViewById(R.id.item_number);
			holder.item_increase = (Button) convertView
					.findViewById(R.id.item_increase);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		convertView.setLayoutParams(lParams);
		holder.item_serial.setText(mList.get(position).get("serial"));
		holder.item_content.setText(mList.get(position).get("content"));
		holder.item_number.setText(mList.get(position).get("number"));
		holder.item_reduce.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mList.get(position).put(
						"number",""+((Integer.parseInt(mList.get(position).get("number"))) - 1));
				notifyDataSetChanged();
			}
		});
		holder.item_increase.setOnClickListener(new OnClickListener() {

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

				mList.get(position).put(
						"number","" + ((Integer.parseInt(mList.get(position).get("number"))) + 1));
				notifyDataSetChanged();
			}
		});
		return convertView;
	}

}

private class ViewHolder {
	TextView item_serial;
	TextView item_content;
	Button item_reduce;
	TextView item_number;
	Button item_increase;
}

}
。。。

posted @ 2017-05-18 11:13  张佳慧1502720137  阅读(3279)  评论(0编辑  收藏  举报