zoukankan      html  css  js  c++  java
  • 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;
    }
    

    }
    。。。

  • 相关阅读:
    2014第2周四部署环境&买火车票
    2014第2周三Web安全学习
    2014第2周二
    2014第2周一
    2013第一周日小结
    2013第1周六意外加班到很晚
    2014第一周五开发问题记URL传参乱码等
    XML文档形式&JAVA抽象类和接口的区别&拦截器过滤器区别
    通过WriteProcessMemory改写进程的内存
    【java】Windows7 下设置环境变量
  • 原文地址:https://www.cnblogs.com/zjh55/p/6872357.html
Copyright © 2011-2022 走看看