ListView-item中的控件-点击事件【购物车物品数量加减】

部分代码从其他博客copy过来的,【数据-Map集合】

-------------------------------------------------------------------------------------------------------------------

1.主要实现商品数量加减

2.上下拖动item不会影响数量

3.点击某个控件不会出现其他item的数量变化

-------------------------------------------------------------------------------------------------------------------

ListView-item中的控件-点击事件【购物车物品数量加减】_第1张图片

-------------------------------------------------------------------------------------------------------------------


package com.example.tylxdz;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


/**
 * 购物车列表
 * 主要功能: 1.ListView加载购物车列表
 * 2.实现不同item的数据操作
 * 3.增减数量
 * @author hlzby
 * 
 */
public class ShoppingViewActivity extends Activity {


ListView shop_lv;
// -
ShoppingAdapter adapter;
// -
private List> mData;
public static String title[] = new String[] { "菜名0", "菜名1", "菜名2", "菜名3",
"菜名4", "菜名5", "菜名6", "菜名7", "菜名8", "菜名9" };
public static String info[] = new String[] { "¥:8", "¥:18", "¥:28", "¥:38",
"¥:48", "¥:58", "¥:68", "¥:78", "¥:88", "¥:98" };
public static String num[] = new String[] { "1", "2", "3", "4",
"1", "1", "1", "1", "1", "1" };


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopview);
mData = getData();
init();
}


private void init() {
shop_lv = (ListView) findViewById(R.id.shop_lv);
ImageButton ibtn_toA = (ImageButton) findViewById(R.id.ibtn_toA);
adapter = new ShoppingAdapter(this);
shop_lv.setAdapter(adapter);
ibtn_toA.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
finish();
}
});
}


private List> getData() {
List> list = new ArrayList>();
for (int i = 0; i < title.length; i++) {
Map map = new HashMap();
map.put("title", title[i]);
map.put("info", info[i]);
list.add(map);
}


return list;
}


// -
public class ShoppingAdapter extends BaseAdapter {


private Context context;
private LinearLayout layout;


public ShoppingAdapter(Context context) {
this.context = context;
}


// 提取出来方便点
public final class ViewHolder {
public TextView huo_tv1;
public TextView huo_tv2;
public ImageView btn_sub;
public ImageView btn_add;
EditText et_btn;
}


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


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


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


// ****************************************final方法
// 注意原本getView方法中的int position变量是非final的,现在改为final
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
System.out.println("手动刷新执行...");
ViewHolder holder = null;
LayoutInflater inflater = LayoutInflater.from(context);
layout = (LinearLayout) inflater.inflate(R.layout.shop_adapter,
null);


holder = new ViewHolder();
//TextView内容控件初始化
holder.huo_tv1 = (TextView) layout.findViewById(R.id.huo_tv1);
holder.huo_tv2 = (TextView) layout.findViewById(R.id.huo_tv2);
//ImageView内容控件初始化
holder.btn_sub = (ImageView) layout.findViewById(R.id.btn_sub);
holder.btn_add = (ImageView) layout.findViewById(R.id.btn_add);
//EditText内容控件初始化
holder.et_btn = (EditText) layout.findViewById(R.id.et_btn);
layout.setTag(holder);
holder = (ViewHolder) layout.getTag();


holder.huo_tv1.setText((String) mData.get(position).get("title"));
holder.huo_tv2.setText((String) mData.get(position).get("info"));
holder.et_btn.setText(num[position].toString());
holder.btn_sub.setTag(position);
holder.btn_add.setTag(position);
// 给Button添加单击事件 添加Button之后ListView将失去焦点 需要的直接把Button的焦点去掉
holder.btn_sub.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
showInfo_sub(position);
}
});
holder.btn_add.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
showInfo_add(position);
}
});
return layout;
}


/**
* 购物车减一数量的逻辑
* @param position
*/
public void showInfo_sub(int position) {
Integer i = Integer.valueOf(num[position].toString());
if (i == 0) {


} else {
int j = --i;
num[position] = String.valueOf(j);
adapter.notifyDataSetChanged();
}
}


/**
* 购物车加一数量的逻辑
* @param position
*/
public void showInfo_add(int position) {
Integer i = Integer.valueOf(num[position].toString());
int j = ++i;
num[position] = String.valueOf(j);
adapter.notifyDataSetChanged();
}
}
// -


}

你可能感兴趣的:(Android系统控件)