我使用CursorAdapter填充了自定义ListView的以下实现:
private class CurAdapter extends CursorAdapter{
public CurAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ViewHolder holder = (ViewHolder) view.getTag();
String name = (cursor.getString(cursor.getColumnIndexOrThrow("NotificationDateFor")));
String image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));
holder.nametext.setText(name);
setImage(image, holder.iv);
holder.chk.setOnClickListener(onchk);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.group_list, null);
ViewHolder holder = new ViewHolder(view);
view.setTag(holder);
return view;
}
private View.OnClickListener onchk = new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};
public class ViewHolder {
TextView nametext;
RoundedImageView iv;
CheckBox chk;
public ViewHolder(View view){
iv = (RoundedImageView)view.findViewById(R.id.imageView2);
nametext = (TextView) view.findViewById(R.id.textView1);
chk = (CheckBox) view.findViewById(R.id.checkBox1);
}
}
}
我正在使用Holder模式以避免重复,但是在滚动列表时我随机检查了复选框,我在这里想念什么?
此外,我想知道如何在复选框的onClick中存储单击复选框的值。
答案 0 :(得分:0)
只需创建一个Android项目并逐步执行此操作:
<强> activity_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" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showDialog"
android:text="Click Here..." />
<TextView
android:id="@+id/techView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<强> MainActivity.java 强>
package com.example.multichoiceitem;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
final String[] items = { ".NET", "J2EE", "PHP", "Android", "DBMS", "JAVA",
"C", "C++" };
String[] itemstemp;
boolean[] selected = new boolean[items.length];
String selectedTech = "Selected Tech - ";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemstemp = new String[items.length];
itemstemp[0] = ".NET";
itemstemp[1] = "Android";
for (int i = 2; i < items.length; i++) {
itemstemp[i] = "";
}
for (int i = 0; i < items.length; i++) {
for (int j = 0; j < itemstemp.length; j++) {
if (items[i].equals(itemstemp[j])) {
selected[i] = true;
}
}
}
}
public void showDialog(View v) {
selectedTech = "";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a Language");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < items.length; i++) {
if (selected[i]) {
selectedTech = selectedTech + items[i] + " ";
selected[i] = true;
}
}
TextView tv = (TextView) findViewById(R.id.techView);
tv.setText(selectedTech);
}
});
builder.setMultiChoiceItems(items, selected,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
selected[which] = isChecked;
}
});
builder.show();
}
}
<强>清单:强>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.multichoiceitem"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是mulchoice项目,它包含复选框。 如果你遇到任何问题,请运行代码问我。 这是一个非常简单的过程..
答案 1 :(得分:0)
你创建了一个类似的
List<Integer> positionList
和适配器getview方法
checbox.setCheckedChanged method in
positionList.add(position);
和
for(Integer int:positionList){
if(int==position){
yourCheckBox.setChecked(true);
} }
答案 2 :(得分:0)
我知道这个问题很老但这可能对某些人有帮助。
如果您要延长SimpleCursorAdapter
,您会注意到ListView
项目内的文字视图不会重复。只有复选框。在适配器的类中,您需要:
private ArrayList<Boolean> checkBoxStates = new ArrayList<>();
在构造函数中,放置:
for(int i = 0; i < getCount(); i++) {
checkBoxStates.add(i, false);
}
并覆盖getView()
方法,如下所示:
public View getView(final int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
CheckBox checkBox = view.findViewById(R.id.checkbox);
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
checkBoxStates.set(position, true);
}else{
checkBoxStates.set(position, false);
}
}
});
checkBox.setChecked(checkBoxStates.get(position));
return view;
}