@AS—ListView(购物车)
#效果展示:
#上代码:
主界面MainActivity:
private int count = 0 ;
private SharedPreferences sp;
private SharedPreferences.Editor editor;
private Button submit;
private ListView listView;
private List<Map<String,Object>> list;
private Integer[] imageIDs = {R.mipmap.shaseng,R.mipmap.sunwukong,R.mipmap.tangseng,R.mipmap.zhubajie,R.mipmap.guanyin};
private String[] name = {"悟净","悟空","长老","悟能","菩萨"};
private String[] details = {"悟净:大师兄,师傅又被妖怪抓走了.",
"悟空:妖怪,吃俺老孙一棒.","长老:来世若有缘分,再与你相恋.",
"悟能:猴哥,我要回高老庄了.","菩萨:八百里黄泉种满曼陀沙华."};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//用SharedPreferences实现进入该应用次数
//获取实例
sp = getSharedPreferences("info",MODE_PRIVATE);
editor = sp.edit();
count = sp.getInt("count",0);
count++;
editor.putInt("count",count);
editor.commit();
Toast.makeText(this,"这是您的第"+count+"次光临!感谢您的支持!",Toast.LENGTH_SHORT).show();
submit = findViewById(R.id.btn_submit);
submit.setOnClickListener(new ClickEvent());
listView = findViewById(R.id.lv_shopcart);
list = new ArrayList<>();
for(int i = 0; i < name.length; i++){
Map<String,Object> map = new HashMap<String,Object>();
map.put("imageID",imageIDs[i]);
map.put("tv","物品名称:");
map.put("name",name[i]);
map.put("detail",details[i]);
list.add(map);
}
ShopAdapter adapter = new ShopAdapter(this,list);
listView.setAdapter(adapter);
}
private class ClickEvent implements View.OnClickListener {
public void onClick(View v) {
// TODO Auto-generated method stub
String goodsList = "";
for(int i = 0; i < list.size(); i++) {
goodsList += ShopAdapter.hasChecked(i)? name[i] + " ": "";
}
new AlertDialog.Builder(MainActivity.this)
.setTitle("购物清单:")
.setMessage("你好,你选择了如下商品:\n\n" + " "+goodsList)
.setPositiveButton("确定", null)
.show();
}
}
适配器:
public class ShopAdapter extends BaseAdapter {
private Context context;//运行上下文
private List<Map<String,Object>> data;//商品信息数据
private static boolean[] hasChecked;//记录商品选中状态
public ShopAdapter(Context context, List<Map<String, Object>> data) {
this.context = context;
this.data = data;
hasChecked = new boolean[getCount()];
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View contentView, ViewGroup parent) {
final int selectID = position;
ViewHolder viewHolder = null;
//第一次加载
if(contentView == null){
viewHolder = new ViewHolder();
//加载布局
contentView = LayoutInflater.from(context).inflate(R.layout.list_item,null);
//赋值
viewHolder.imageView = contentView.findViewById(R.id.imageView);
viewHolder.tv = contentView.findViewById(R.id.tv);
viewHolder.tv_name = contentView.findViewById(R.id.tv_name);
viewHolder.check = contentView.findViewById(R.id.item_cbx);
viewHolder.btn = contentView.findViewById(R.id.button);
contentView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) contentView.getTag();
}
//设置数据
viewHolder.imageView.setImageResource((Integer) data.get(position).get("imageID"));
viewHolder.tv.setText((String) data.get(position).get("tv"));
viewHolder.tv_name.setText((String) data.get(position).get("name"));
viewHolder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(context)
.setIcon((Integer) data.get(position).get("imageID"))
.setTitle("物品详情:"+ (String) data.get(position).get("name"))
.setMessage((String) data.get(position).get("detail"))
.setPositiveButton("确定",null)
.create().show();
}
});
//注册多选框状态事件处理
viewHolder.check.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//记录物品选中状态
checkedChange(selectID);
}
});
return contentView;
}
public class ViewHolder {
private ImageView imageView;
private TextView tv;
private TextView tv_name;
private CheckBox check;
private Button btn;
}
//记录勾选了哪个物品
private void checkedChange(int checkedID){
hasChecked[checkedID] = !hasChecked[checkedID];
}
//判断物品是否选择
public static boolean hasChecked(int checkedID){
return hasChecked[checkedID];
}
}
主界面布局:
<LinearLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:gravity="center"
android:textSize="25sp"
android:text="毛绒玩具公仔购物车" />
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="280dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
android:text="提交"
android:textSize="20sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
android:text="商品列表 :" />
<ListView
android:id="@+id/lv_shopcart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp" />
</LinearLayout>
如图所示:
item_list:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_launcher_foreground" />
<LinearLayout
android:layout_width="170dp"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="物品名称:" />
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp" />
</LinearLayout>
<CheckBox
android:id="@+id/item_cbx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" " />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="商品详情" />
如图所示: