java – Android – Checkbox在listview中每10次重复一次
发布时间:2020-12-15 02:10:05 所属栏目:Java 来源:网络整理
导读:您好我一直在尝试使用自定义适配器创建列表视图,以在其中放置两个textview和一个复选框.列表视图工作正常,但复选框开始每10秒重复一次,所以如果你检查第一个复选框,第10个复选框也会检查,依此类推.我对此进行了研究,但找不到具体的答案,这个答案得到了很好的
您好我一直在尝试使用自定义适配器创建列表视图,以在其中放置两个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,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,int flags) { super(context,0); checkbox = new boolean[cursor.getCount()]; } @Override public View newView(Context context,ViewGroup parent) { return LayoutInflater.from(context).inflate(R.layout.act_view_items_view,false); } @Override public void bindView(View view,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; }}); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |