这是listview的适配器代码:
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  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;
    }
 }activity的代码public class SecondActivity 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.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"));        }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) {
        MyListViewAdapter.ViewHolder holder = (MyListViewAdapter.ViewHolder) view.getTag();
        MyListViewAdapter.getIsSelected().put(position,holder.cb.isChecked());    }
}item的布局代码<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">    <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="32dp"
        android:background="#ffffff"
        tools:srcCompat="@drawable/m" />    <TextView
        android:id="@+id/food_name"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical"
        android:text="咖啡"
        android:textSize="18sp" />    <TextView
        android:layout_width="94dp"
        android:layout_height="14dp"
        android:paddingLeft="20dp"
        android:text="单价:RMB"
        android:textSize="12sp" />    <TextView
        android:id="@+id/price"
        android:layout_width="43dp"
        android:layout_height="20dp"
        android:layout_marginLeft="20dp"
        android:paddingEnd="10dp"
        android:text="18"
        android:textSize="18sp" /></LinearLayout>main的布局代码<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>结果却是这样

解决方案 »

  1.   

    listivew     
    android:layout_width="match_parent"
        android:layout_height="match_parent"item    android:layout_width="match_parent"
        android:layout_height="wrap_content"
      

  2.   


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

  3.   

    打印下initData()数据是否为空
      

  4.   

    出现问题第一条:看日志
    第二条:debug先debug你的foods有没有数据再说吧