【android】checkedTextViewはカスタムListViewを形成しています。
7451 ワード
ネット上のものは全部禁止されているようです。以下は私が作ったこの方法が可能です。
まず、
main.xml
まず、
main.xml
<?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;
}
カスタムアドホッターメソッド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);
}
}