dimanche 25 septembre 2016

Accessing ListView CheckBox in Android

I'm making a to-do list app in android. Everything is working well enough except I have checkboxes in a ListView that I cannot set programmatically. I'm using a SQLite database that stores my tasks and whether or not they have been marked as complete. When I check a checkbox, it successfully saves a "1" in my database. I have an updateUI method that reads the text of my tasks from my database and displays it. I have made a new "updateUICheckBox" method to handle updating the UI for the checkboxes. Right now I'm just trying to set all my checkboxes as checked, but I'm getting a nullpointerexception when I call setChecked(true).

// not working
private void updateUICheckBox() {
    CheckBox c = (CheckBox) findViewById(R.id.list_complete_check_box);
    c.setChecked(true); // null pointer exception
}

Here's my MainActivity (everything works except updateUICheckbox):

public class MainActivity extends AppCompatActivity {
private DbHelper mDbHelper;
private ListView mListView;
private ArrayAdapter<String> mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDbHelper = new DbHelper(this);
    mListView = (ListView) findViewById(R.id.list_todo);

    updateUI();
    updateUICheckBox();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.add_button, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId()) {
        case R.id.menu_item_new_task:
            Intent intent = new Intent(this, SecondActivity.class);
            this.startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onResume() {
    super.onResume();
    updateUI();
    updateUICheckBox();
}

private void updateUI() {
    ArrayList<String> tasks = new ArrayList<>();
    SQLiteDatabase db = mDbHelper.getReadableDatabase();

    Cursor cursor = db.query(Schema.TaskTable.TABLE, new String[]{Schema.TaskTable._ID, Schema.TaskTable.COL_TASK_TITLE},
            null, null, null, null, null);
    while (cursor.moveToNext()) {
        int idx = cursor.getColumnIndex(Schema.TaskTable.COL_TASK_TITLE);
        tasks.add(cursor.getString(idx));
    }

    if (mAdapter == null) {
        mAdapter = new ArrayAdapter<>(this, R.layout.list_item, R.id.list_task, tasks);
        mListView.setAdapter(mAdapter);
    } else {
        mAdapter.clear();
        mAdapter.addAll(tasks);
        mAdapter.notifyDataSetChanged();
    }

    cursor.close();
    db.close();
}

// not working
private void updateUICheckBox() {
    CheckBox c = (CheckBox) findViewById(R.id.list_complete_check_box);
    c.setChecked(true); // null pointer exception
}

public void deleteTask(View view) {
    View parent = (View) view.getParent();
    TextView taskTextView = (TextView) parent.findViewById(R.id.list_task);
    String task = String.valueOf(taskTextView.getText());
    SQLiteDatabase db = mDbHelper.getWritableDatabase();
    db.delete(Schema.TaskTable.TABLE,
            Schema.TaskTable.COL_TASK_TITLE + " = ?",
            new String[]{task});
    db.close();
    updateUI();
    updateUICheckBox();
}

public void addCheck(View view) {
    CheckBox checkBox = (CheckBox)view;
    if(checkBox.isChecked()){
        View parent = (View) view.getParent();
        TextView taskTextView = (TextView) parent.findViewById(R.id.list_task);
        String col_id = String.valueOf(taskTextView.getText());

        SQLiteDatabase db = mDbHelper.getWritableDatabase();

        String strSQL = "UPDATE Tasks SET complete = 1 WHERE title = " + "'" + col_id + "';";

        db.execSQL(strSQL);
        db.close();

        updateUI();
        updateUICheckBox();
    }
    else
    {
        View parent = (View) view.getParent();
        TextView taskTextView = (TextView) parent.findViewById(R.id.list_task);
        String col_id = String.valueOf(taskTextView.getText());

        SQLiteDatabase db = mDbHelper.getWritableDatabase();

        String strSQL = "UPDATE Tasks SET complete = 0 WHERE title = " + "'" + col_id + "';";

        db.execSQL(strSQL);
        db.close();

        updateUI();
        updateUICheckBox();
    }
}

}

Here is my main activity's layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
            xmlns:tools="http://ift.tt/LrGmb4"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

<ListView
    android:id="@+id/list_todo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

And here's the layout for the ListView items:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:tools="http://ift.tt/LrGmb4">

<CheckBox
    android:id="@+id/list_complete_check_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="4dp"
    android:scaleX="1.2"
    android:scaleY="1.2"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="addCheck"
    />

<TextView
    android:id="@+id/list_task"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/list_complete_check_box"
    android:textStyle="bold"
    tools:text="Task"
    android:padding="4dp"
    android:textSize="15sp"
    android:layout_alignBottom="@+id/list_complete_check_box"/>

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="X"
    android:id="@+id/button"
    android:textSize="10sp"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:layout_alignBottom="@+id/list_complete_check_box"
    android:layout_toLeftOf="@+id/list_complete_check_box"
    android:layout_toStartOf="@+id/list_complete_check_box"
    android:onClick="deleteTask"/>
</RelativeLayout>




Aucun commentaire:

Enregistrer un commentaire