您好我一直在尝试使用自定义适配器创建列表视图,以在其中放置两个textvIEw和一个复选框.列表视图工作正常,但复选框开始每10秒重复一次,所以如果你检查第一个复选框,第10个复选框也会检查,依此类推.我对此进行了研究,但找不到具体的答案,这个答案得到了很好的解释,我可以理解我必须做些什么来解决它.那么如何修复复选框以便它们不重复?
act_vIEw_items_vIEw.xml
<relativeLayout androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" xmlns:androID="http://schemas.androID.com/apk/res/androID"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="New Text" androID:ID="@+ID/workout_items_textVIEw" androID:layout_alignParenttop="true" androID:layout_alignParentleft="true" androID:layout_alignParentStart="true" androID:textSize="20dp" androID:gravity="center_vertical" androID:layout_toStartOf="@+ID/workout_items_textVIEw2" androID:layout_toleftOf="@+ID/workout_items_textVIEw2" androID:textIsSelectable="false" androID:height="50dp" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="New Text" androID:ID="@+ID/workout_items_textVIEw2" androID:textSize="20dp" androID:gravity="center_vertical" androID:layout_toleftOf="@+ID/workout_items_CheckBox" androID:layout_alignParenttop="true" androID:height="50dp"/> <CheckBox androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/workout_items_CheckBox" androID:layout_alignParenttop="true" androID:layout_alignParentRight="true" androID:layout_alignParentEnd="true" androID:height="40dp" androID:layout_alignBottom="@+ID/workout_items_textVIEw2" androID:focusable="false"/></relativeLayout>
vIEw_items.java
public class vIEw_items extends ListActivity { private listadapter listadapter; private listadapter listadapter2; private TaskDBHelper helper; private TextVIEw taskTextVIEw; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.act_vIEw_items); updateUI2(); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); return true; } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { switch (item.getItemID()) { case R.ID.action_add_task: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Add a Workout"); builder.setMessage("Enter the name of the workout"); final EditText inputFIEld = new EditText(this); builder.setVIEw(inputFIEld); builder.setPositivebutton("Next", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialogInterface, int i) { AlertDialog.Builder builder2 = new AlertDialog.Builder(vIEw_items.this); builder2.setTitle("Add a Workout"); builder2.setMessage("Enter the reps for the workout"); final EditText inputFIEld2 = new EditText(vIEw_items.this); builder2.setVIEw(inputFIEld2); builder2.setPositivebutton("Next", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialogInterface, int i) { String task = inputFIEld.getText().toString(); String reps = inputFIEld2.getText().toString(); String workoutID = TaskContract.UNStable_WORKOUT_ID; helper = new TaskDBHelper(vIEw_items.this); sqliteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.clear(); values.put(TaskContract.Columns.TASK, task); values.put(TaskContract.Columns.REPS, reps); values.put(TaskContract.Columns.WORKOUT_ID, workoutID); db.insertWithOnConflict(TaskContract.table, null, values, sqliteDatabase.CONFliCT_IGnorE); updateUI2(); } }); builder2.setNegativebutton("Cancel", null); builder2.create().show(); } }); builder.setNegativebutton("Cancel",null); builder.create().show(); return true; default: return false; } } public voID updateUI2(){ // TodoDatabaseHandler is a sqliteOpenHelper class connecting to sqlite TaskDBHelper handler = new TaskDBHelper(this); // Get access to the underlying writeable database sqliteDatabase db = handler.getWritableDatabase(); // query for items from the database and get a cursor back String sql2 = String.format("SELECT * FROM workout_items WHERE %s = '%s'", TaskContract.Columns.WORKOUT_ID, TaskContract.UNStable_WORKOUT_ID); Cursor todoCursor = db.rawquery(sql2, null); // Find ListVIEw to populate setContentVIEw(R.layout.act_vIEw_items); ListVIEw lvItems = (ListVIEw) findVIEwByID(androID.R.ID.List); // Setup cursor adapter using cursor from last step vIEw_items_CursorAdapter todoAdapter = new vIEw_items_CursorAdapter(this, todoCursor, 0); // Attach cursor adapter to the ListVIEw lvItems.setAdapter(todoAdapter); } public voID onDonebuttonClick(VIEw vIEw) { VIEw v = (VIEw) vIEw.getParent(); TextVIEw taskTextVIEw = (TextVIEw) v.findVIEwByID(R.ID.taskTextVIEw); String task = taskTextVIEw.getText().toString(); String sql = String.format("DELETE FROM %s WHERE %s = '%s'", TaskContract.table, TaskContract.Columns.TASK, task); helper = new TaskDBHelper(vIEw_items.this); sqliteDatabase sqlDB = helper.getWritableDatabase(); sqlDB.execsql(sql); updateUI2(); }}
vIEw_items_CursorAdapter.java
public class vIEw_items_CursorAdapter extends CursorAdapter { public vIEw_items_CursorAdapter(Context context, Cursor cursor, int flags) { super(context, cursor, 0); } @OverrIDe public VIEw newVIEw(Context context, Cursor cursor, VIEwGroup parent) { return LayoutInflater.from(context).inflate(R.layout.act_vIEw_items_vIEw, parent, false); } @OverrIDe public voID bindVIEw(VIEw vIEw, Context context, Cursor cursor) { TextVIEw tvBody = (TextVIEw) vIEw.findVIEwByID(R.ID.workout_items_textVIEw); TextVIEw tvPriority = (TextVIEw) vIEw.findVIEwByID(R.ID.workout_items_textVIEw2); String body = cursor.getString(cursor.getColumnIndexOrThrow("task")); int priority = cursor.getInt(cursor.getColumnIndexOrThrow("reps")); tvBody.setText(body); tvPriority.setText(String.valueOf(priority)); }}
解决方法:
试试这个.希望这能解决你的问题.
public class vIEw_items_CursorAdapter extends CursorAdapter { boolean[] checkBox; public vIEw_items_CursorAdapter(Context context, Cursor cursor, int flags) { super(context, cursor, 0); checkBox = new boolean[cursor.getCount()];}@OverrIDepublic VIEw newVIEw(Context context, Cursor cursor, VIEwGroup parent) { return LayoutInflater.from(context).inflate(R.layout.act_vIEw_items_vIEw, parent, false);}@OverrIDepublic voID bindVIEw(VIEw vIEw, Context context, Cursor cursor) { final int position = cursor.getposition(); TextVIEw tvBody = (TextVIEw) vIEw.findVIEwByID(R.ID.workout_items_textVIEw); TextVIEw tvPriority = (TextVIEw) vIEw.findVIEwByID(R.ID.workout_items_textVIEw2); CheckBox tvCheck = (CheckBox) vIEw.findVIEwByID(R.ID.checkBox1); String body = cursor.getString(cursor.getColumnIndexOrThrow("task")); int priority = cursor.getInt(cursor.getColumnIndexOrThrow("reps")); tvBody.setText(body); tvPriority.setText(String.valueOf(priority)); tvCheck.setChecked(checkBox[position]); tvCheck.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() { @OverrIDe public voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) { checkBox[position] = isChecked; }});}
总结 以上是内存溢出为你收集整理的java – Android – Checkbox在listview中每10次重复一次全部内容,希望文章能够帮你解决java – Android – Checkbox在listview中每10次重复一次所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)