|
这个文件是Activity的布局文件:
java代码: <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
/>
</LinearLayout>
复制代码有了上面的两个文件,就可以写Activity了
下面的是源码:
java代码: 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.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.heji.demo.R;
public class MultipleChoiceActivity extends Activity implements OnItemClickListener{
private MyAdapter mSimpleAdapter;
public final static String NAME = "name";
public final static String PHONE_NUMBER = "phone";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("带有多选的Listview");
setContentView(R.layout.multiple_checkbox_main);
ListView listView = (ListView) findViewById(R.id.listview);
ArrayList<Map<String, String>> al = new ArrayList<Map<String,String>>();
Map<String, String> map1 = new HashMap<String, String>();
map1.put(NAME, "A");
map1.put(PHONE_NUMBER, "132456789");
al.add(map1);
Map<String, String> map2 = new HashMap<String, String>();
map2.put(NAME, "B");
map2.put(PHONE_NUMBER, "132134");
al.add(map2);
Map<String, String> map3 = new HashMap<String, String>();
map3.put(NAME, "C");
map3.put(PHONE_NUMBER, "132134");
al.add(map3);
Map<String, String> map4 = new HashMap<String, String>();
map4.put(NAME, "D");
map4.put(PHONE_NUMBER, "132134");
al.add(map4);
Map<String, String> map5 = new HashMap<String, String>();
map5.put(NAME, "E");
map5.put(PHONE_NUMBER, "132134");
al.add(map5);
Map<String, String> map6 = new HashMap<String, String>();
map6.put(NAME, "F");
map6.put(PHONE_NUMBER, "132134");
al.add(map6);
Map<String, String> map7 = new HashMap<String, String>();
map7.put(NAME, "G");
map7.put(PHONE_NUMBER, "132134");
al.add(map7);
Map<String, String> map8 = new HashMap<String, String>();
map8.put(NAME, "H");
map8.put(PHONE_NUMBER, "132134");
al.add(map8);
Map<String, String> map9 = new HashMap<String, String>();
map9.put(NAME, "I");
map9.put(PHONE_NUMBER, "132134");
al.add(map9);
Map<String, String> map10 = new HashMap<String, String>();
map10.put(NAME, "J");
map10.put(PHONE_NUMBER, "132134");
al.add(map10);
Map<String, String> map11 = new HashMap<String, String>();
map11.put(NAME, "K");
map11.put(PHONE_NUMBER, "132134");
al.add(map11);
String[] from = { NAME, PHONE_NUMBER };
int[] to = { R.id.multiple_title, R.id.multiple_summary };
mSimpleAdapter = new MyAdapter(this, al, R.layout.multiple_checkbox_main_row, from, to);
listView.setAdapter(mSimpleAdapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckBox checkBox = (CheckBox) view.findViewById(R.id.multiple_checkbox);
checkBox.toggle();
mSimpleAdapter.map.put(position, checkBox.isChecked());
}
public class MyAdapter extends SimpleAdapter {
Map<Integer, Boolean> map;
LayoutInflater mInflater;
private List<? extends Map<String, ?>> mList;
public MyAdapter(Context context, List<Map<String, String>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
map = new HashMap<Integer, Boolean>();
mInflater = LayoutInflater.from(context);
mList = data;
for(int i = 0; i < data.size(); i++) {
map.put(i, false);
}
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = mInflater.inflate(R.layout.multiple_checkbox_main_row, null);
}
TextView tN = (TextView) convertView.findViewById(R.id.multiple_title);
tN.setText((String)mList.get(position).get(NAME));
TextView tP = (TextView) convertView.findViewById(R.id.multiple_summary);
tP.setText((String)mList.get(position).get(PHONE_NUMBER));
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.multiple_checkbox);
checkBox.setChecked(map.get(position));
return convertView;
}
}
}
复制代码 |
|