网络上的那个东西似乎都不准,以下是我做的此方法为可行。
首先,[b]main.xml[/b]
然后list.xml
以下是自定义组件。
自定义adapter方法
下面是Acitivity
首先,[b]main.xml[/b]
<?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="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
然后list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/_id"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="TextView" />
<CheckedTextView
android:id="@+id/checkText"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>
</RelativeLayout>
以下是自定义组件。
public final class Compan {
public TextView id;
public CheckedTextView ctv;
}
自定义adapter方法
package com.duduli.li;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckedTextView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class MyAdapter extends SimpleCursorAdapter{
private List list = new ArrayList();
public String s[];
private List<Map<String, Object>> data;
private LayoutInflater layoutInflater;
private List<String> delNumber = new ArrayList<String>();
@SuppressWarnings("unused")
private Context context;
public MyAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.data = getData();
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return super.getCount();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return super.getItemId(position);
}
@SuppressWarnings("unused")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Compan cp = null;
if(cp == null){
cp = new Compan();
convertView = layoutInflater.inflate(R.layout.list, null);
cp.id = (TextView) convertView.findViewById(R.id._id);
cp.ctv = (CheckedTextView) convertView.findViewById(R.id.checkText);
convertView.setTag(cp);
}else{
cp = (Compan) convertView.getTag();
}
cp.id.setText((CharSequence) data.get(position).get("_id"));
cp.ctv.setText((CharSequence) data.get(position).get("Number"));
return convertView;
}
public List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
while (getCursor().moveToNext()) {
Map<String, Object> map = new HashMap<String, Object>();
// System.out.println("ID: "+getCursor().getString(0));
map.put("_id", getCursor().getString(0));
// System.out.println("NAME: "+getCursor().getString(1));
map.put("Number", getCursor().getString(1));
list.add(map);
}
return list;
}
}
下面是Acitivity
package com.duduli.li;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckedTextView;
import android.widget.ListView;
import com.duduli.db.AllowCallDAO;
/**
* This example shows how to use choice mode on a list. This list is
* in CHOICE_MODE_MULTIPLE mode, which means the items behave like
* checkboxes.
*/
public class CallComingTest2Activity extends ListActivity {
private CheckedTextView multiple;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AllowCallDAO dao = new AllowCallDAO(this);
MyAdapter adapter = new MyAdapter(this, R.layout.list, dao.show()
, new String[]{"_id","phonenr"}, new int[]{R.id._id,R.id.checkText});
final ListView listView = getListView();
listView.setItemsCanFocus(false);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
System.out.println(position);
// [b]下面的才是最重要的,网络上写的为:multiple = (CheckedTextView)l.getChildAt(position).findViewById(R.id.checkText);此方法很有问题,比如说listview进行翻页之后就会出问题。[/b]
multiple = (CheckedTextView)v.findViewById(R.id.checkText);
if(multiple != null){
System.out.println("not null");
}else{
System.out.println("null");
}
if(multiple.isChecked()){
multiple.setChecked(false);
}else{
multiple.setChecked(true);
}
super.onListItemClick(l, v, position, id);
}
}