Android实战简易教程-第四十七枪(ListView多选-实现点餐系统)

一般在外卖的选单中常常会用到ListView的多选,大家可以改进后加入自己的项目中。下面看一下代码:

一、代码

1.main.xml:(由一个ListView和一个Button组成)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/drink_list"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>

    <Button
        android:id="@+id/btn_commit"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:text="确定" />

</LinearLayout>
2.item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/check_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="true" />

    <ImageView
        android:id="@+id/food_imager"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#ffffff" />

    <TextView
        android:id="@+id/food_name"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="咖啡"
        android:gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:textSize="18sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="单价:RMB  "
        android:paddingLeft="20dp"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:paddingRight="10dp"
        android:text="18"
        android:layout_marginLeft="20dp"
        android:textSize="18sp" />

</LinearLayout>
3.Food.java:(bean类)

package com.example.info;

public class Food {

	public int food_img;
	public String food_name;
	public String food_price;
	public String text;

	public int getFood_img() {
		return food_img;
	}

	public void setFood_img(int food_img) {
		this.food_img = food_img;
	}

	public String getFood_name() {
		return food_name;
	}

	public void setFood_name(String food_name) {
		this.food_name = food_name;
	}

	public String getFood_price() {
		return food_price;
	}

	public void setFood_price(String food_price) {
		this.food_price = food_price;
	}

	public Food(int food_img, String food_name, String food_price) {
		super();
		this.food_img = food_img;
		this.food_name = food_name;
		this.food_price = food_price;
	}

	public Food() {
		super();
	}

	@Override
	public String toString() {
		return super.toString();
	}
}


4.MyListViewAdapter.java:

package com.example.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.info.Food;
import com.example.listviewselectitem.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class MyListViewAdapter extends BaseAdapter {
	// 填充数据的list
	private ArrayList<Food> foodlist;
	// 用来控制CheckBox的选中状况
	private static HashMap<Integer, Boolean> isSelected;
	// 上下文
	private Context context;
	// 用来导入布局
	private LayoutInflater inflater = null;

	// 构造器
	public MyListViewAdapter(ArrayList<Food> list, Context context) {
		this.context = context;
		this.foodlist = list;
		inflater = LayoutInflater.from(context);
		isSelected = new HashMap<Integer, Boolean>();
		// 初始化数据
		initDate();
	}

	// 初始化isSelected的数据
	private void initDate() {
		for (int i = 0; i < foodlist.size(); i++) {
			getIsSelected().put(i, false);
		}
	}

	@Override
	public int getCount() {
		return foodlist.size();
	}

	@Override
	public Object getItem(int position) {
		return foodlist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			// 获得ViewHolder对象
			holder = new ViewHolder();
			// 导入布局并赋值给convertview
			convertView = inflater.inflate(R.layout.item, null);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.food_imager);
			holder.txt1 = (TextView) convertView.findViewById(R.id.food_name);
			holder.txt2 = (TextView) convertView.findViewById(R.id.price);
			holder.cb = (CheckBox) convertView.findViewById(R.id.check_box);
			// 为view设置标签
			convertView.setTag(holder);
		} else {
			// 取出holder
			holder = (ViewHolder) convertView.getTag();
		}
		// 获取数据
		Food food = foodlist.get(position);

		// 将数据填充到当前convertView的对应控件中

		holder.imageView.setImageResource(food.food_img);
		holder.txt1.setText(food.food_name);
		holder.txt2.setText(food.food_price);
		// 设置list中TextView的显示
		// 根据isSelected来设置checkbox的选中状况
		holder.cb.setChecked(getIsSelected().get(position));
		return convertView;
	}

	public static HashMap<Integer, Boolean> getIsSelected() {
		return isSelected;
	}

	public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
		MyListViewAdapter.isSelected = isSelected;
	}

	public static class ViewHolder {
		public TextView txt1;
		public TextView txt2;
		public ImageView imageView;
		public CheckBox cb;
	}
}

5.MainActivity.java:

package com.example.listviewselectitem;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.adapter.MyListViewAdapter;
import com.example.adapter.MyListViewAdapter.ViewHolder;
import com.example.info.Food;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity implements OnClickListener,
		OnItemClickListener {

	private ListView listView;
	private Button ok;
	private ArrayList<Food> foods = new ArrayList<Food>();
	private MyListViewAdapter adapter;
	private CheckBox checkBox;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		initView();// 初始化控件
		View view=LayoutInflater.from(this).inflate(R.layout.item, null);
		checkBox=(CheckBox)view.findViewById(R.id.check_box);

		initData();// 初始化虚拟数据

		adapter = new MyListViewAdapter(foods, getApplicationContext());
		listView.setAdapter(adapter);
	}

	/**
	 * 初始化控件
	 * */
	public void initView() {
		listView = (ListView) findViewById(R.id.drink_list);// listview列表控件
		ok = (Button) findViewById(R.id.btn_commit);// 确定按钮

		ok.setOnClickListener(this);
		listView.setOnItemClickListener(this);
	}

	/**
	 * 初始化虚拟数据
	 * */
	public void initData() {
		
		
		
		
		Class cls = R.drawable.class;//反射
		try {
			foods.add(new Food(cls.getDeclaredField("d1").getInt(null), "猕猴桃汁",
					"10"));
			foods.add(new Food(cls.getDeclaredField("d2").getInt(null), "橙汁",
					"12"));
			foods.add(new Food(cls.getDeclaredField("d3").getInt(null), "啤酒",
					"15"));
			foods.add(new Food(cls.getDeclaredField("d4").getInt(null), "葡萄汁",
					"10"));
			foods.add(new Food(cls.getDeclaredField("d5").getInt(null), "纯麦奶茶",
					"8"));
			foods.add(new Food(cls.getDeclaredField("d6").getInt(null), "薄荷汁",
					"10"));
			foods.add(new Food(cls.getDeclaredField("d7").getInt(null), "柠檬薄荷",
					"12"));
			foods.add(new Food(cls.getDeclaredField("d8").getInt(null), "椰子汁",
					"10"));
			foods.add(new Food(cls.getDeclaredField("d9").getInt(null), "珍珠奶茶",
					"9"));
			foods.add(new Food(cls.getDeclaredField("d10").getInt(null), "石榴汁",
					"10"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 按钮的点击事件处理
	 * */
	@Override
	public void onClick(View v) {
		int mID = v.getId();
		switch (mID) {
		case R.id.btn_commit:
			myPrice();// 计算总价并输出
			break;
		
		}
	}

	/**
	 * 计算总价格的方法
	 * */
	public void myPrice() {
		HashMap<Integer, Boolean> map = MyListViewAdapter.getIsSelected();
		String str = "";
		int money = 0;
		for (int i = 0; i < map.size(); i++) {
			if (map.get(i)) {
				str += (i + " ");
				money += Integer.parseInt(foods.get(i).food_price);
			}
		}
		MyListViewAdapter.getIsSelected().get("");
		Toast.makeText(getApplicationContext(),
				"已选中了" + str + "项,总价钱为:" + money, Toast.LENGTH_SHORT).show();
	}

	/**
	 * listview的item的选择的方法
	 * */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
		ViewHolder holder = (ViewHolder) view.getTag();
		// 改变CheckBox的状态
		holder.cb.toggle();
		// 将CheckBox的选中状况记录下来
		MyListViewAdapter.getIsSelected().put(position, holder.cb.isChecked());

	}
}

二、运行实例



喜欢的朋友关注我和我的公众号(左边栏)!谢谢
源码下载


你可能感兴趣的:(Android实战简易教程-第四十七枪(ListView多选-实现点餐系统))