为什么80%的码农都做不了架构师?>>>
1.首先创建一个适配器类CheckBoxListViewAdapter,此类继承BaseAdapter类:
package com.test;
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.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 CheckBoxListViewAdapter extends BaseAdapter {
//Inflater英文意思是膨胀,在Android中应该是扩展的意思吧。LayoutInflater的作用类似于
//findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!
//而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
private LayoutInflater mInflater;
private List
2.接下来定义布局文件my_data.xml,此布局文件中定义是在listView显示的组件信息
3.接下来定义my_list.xml显示布局文件
4.接下来就是TestQActivity
package com.test;
import com.test.CheckBoxListViewAdapter.ViewHolder;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class TestQActivity extends Activity {
/** Called when the activity is first created. */
private ListView listview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_list);
listview =(ListView)findViewById(R.id.lv);
CheckBoxListViewAdapter checkBoxListViewAdapter =new CheckBoxListViewAdapter(this);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listview.setItemsCanFocus(false);
listview.setAdapter((ListAdapter) checkBoxListViewAdapter);
listview.setOnItemClickListener(clickListener);
listview.getCheckedItemPositions();
}
private OnItemClickListener clickListener =new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
ViewHolder vHollder = (ViewHolder) view.getTag();
//在每次获取点击的item时将对于的checkbox状态改变,同时修改map的值。
vHollder.cBox.toggle();
CheckBoxListViewAdapter.isSelected.put(position, vHollder.cBox.isChecked());
}
};
}
运行效果如下: