[英]Checkbox with Custom listview
我正在使用cursoradapter從數據庫中獲取值並將其顯示在listview中。我能夠在listview中顯示數據庫內容,同時也顯示perticular項的onclick我獲得了被單擊項的值。現在我還想要復選框,以便oncheck我應該獲取選中項目的值(在這種情況下,復選框click和listitemclick都應該起作用)是否可以? 怎么做?
private void displayListView() {
final Cursor cursor = dbHelper.fetchAllRecords();
String[] columns = new String[] {
RecordsDbAdapter.KEY_NAME,
RecordsDbAdapter.KEY_BIRTHDAY,
};
int[] to = new int[] {
R.id.name,
R.id.birthdate,
};
dataAdapter = new SimpleCursorAdapter(
this, R.layout.rownew,
cursor,
columns,
to);
View v = getLayoutInflater().inflate(R.layout.customdialog, null);
ListView listView = (ListView) v.findViewById(R.id.listChildren);
final EditText etChild = (EditText) v.findViewById(R.id.etChild);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
name = ((TextView) view.findViewById(R.id.name)) .getText().toString();
BirtDate = ((TextView) view.findViewById(R.id.birthdate)) .getText().toString();
Log.d("*************", name);
Info=name+ " " +BirtDate;
Log.d("nameeeeeeeeeeeeeeee",Info);
etChild.setText(new StringBuilder().append(Info));
topaste=etChild.getText().toString();
etChild.setText(new StringBuilder().append(Info1));
// topaste1=etChild.getText().toString();
// }
}
})
我的rownew.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="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#FFFFFF"
android:gravity="center"
android:padding="6dp" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/birthdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/text"
android:textStyle="bold" >
</TextView>
</LinearLayout>
我的Customdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/etChild"
android:hint=""
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<ListView
android:id="@+id/listChildren"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
</ListView>
</LinearLayout>
您可以擴展SimpleCursorAdapter
來為復選框添加點擊偵聽器。 這是我使用的代碼片段:
public class MultiSelectCursorAdapter extends SimpleCursorAdapter {
public MultiSelectCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int fposition = position;
View view = super.getView(position, convertView, parent);
CheckBox checkbox = (CheckBox) view.findViewById(R.id.list_checkbox);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("CHECKER", "Something happened: " + fposition + " was clicked " + isChecked);
// TODO: store id of checked items in your model
}
});
return view;
}
}
在列表行布局中,您需要設置android:focusable="false"
以便允許單擊列表項以及單擊每個列表項的復選框。
<CheckBox
android:id="@+id/list_checkbox"
android:focusable="false"
... >
</CheckBox>
最后,您需要一個模型或數據結構來存儲列表中所有選中的項,例如,存儲所有選中項的標識符的數組或列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.