Hello I've been trying to create a listview with a custom adapter to put two textviews and one checkbox in it. The listviews work fine but the checkboxes start to repeat every 10th so if you check the first checkbox the 10th one also checks and so on. I did research about this but couldn't find a specific answer that was well explained that I could understand what exactly I had to do to fix it. So how do I fix the checkboxes so that they dont repeat?
act_view_items_view.xml
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://ift.tt/nIICcg">
<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));
}
}
Aucun commentaire:
Enregistrer un commentaire