0

I'm trying to make a ToDo app that displays User's Inputted task name and task description within a custom list item. This data will come from an SQLite Database.

The problem I'm facing is that I don't know how to place both the task name and task description into this custom list item as all attempts ive made just show a blank screen.

This is the custom list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/colorWhite">

        <TextView
            android:id="@+id/task_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="55dp"
            android:layout_marginTop="25dp"
            android:text="TextView" />

        <View
            android:id="@+id/viewbutton"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="21dp"
            android:layout_marginTop="24dp"
            android:background="@layout/round" />

    <TextView
        android:id="@+id/task_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/task_name"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="55dp"
        android:layout_marginTop="53dp"
        android:layout_marginBottom="10dp"
        android:text="Undefined"
        android:textStyle="bold" />

</RelativeLayout>

Here is my DatabaseHelper.java

public class  DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "task_table";
private static final String COL1 = "ID";
private static final String COL2 = "name";
private static final String COL3 = "priority";
private static final String COL4 = "desc";

public DatabaseHelper(Context context) {
    super(context, TABLE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COL2 +" TEXT, " + COL3 + " INTEGER, '" + COL4 + "' TEXT )";
    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean addData(String item, String desc) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, item);
    contentValues.put(COL4, desc);

    Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
    long result = db.insert(TABLE_NAME, null, contentValues);

    if (result == -1) {
        return false;
    } else {
        return true;
    }
}
    public ArrayList<String> getTaskList(){
        ArrayList<String> taskList = new ArrayList<>();
        SQLiteDatabase td = this.getReadableDatabase();
        Cursor cursor = td.query(TABLE_NAME,new String[]{COL2},null,null,null,null,null);
        while(cursor.moveToNext()){
            int index = cursor.getColumnIndex(COL2);
            taskList.add(cursor.getString(index));
        }
        cursor.close();
        td.close();
        return taskList;
    }
    public Cursor getData(){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_NAME;
        Cursor data = db.rawQuery(query, null);
        return data;
    }
    public Cursor getItemID(String name){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
                " WHERE " + COL2 + " = '" + name + "'";
        Cursor data = db.rawQuery(query, null);
        return data;
    }

And here is my ListDataActivity.java

public class ListDataActivity extends AppCompatActivity {

private static final String TAG = "ListDataActivity";

DatabaseHelper mDatabaseHelper;
TextView mTaskName;
TextView mTaskDesc;
CustomAdapter customAdapter;
ArrayAdapter<String> adapter;
private ListView mListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout);
    mListView = (ListView) findViewById(R.id.listView);
    mDatabaseHelper = new DatabaseHelper(this);
    mTaskDesc = (TextView) findViewById(R.id.task_desc);
    mTaskName = (TextView) findViewById(R.id.task_name);

    populateListView();
}

private void populateListView() {
    Log.d(TAG, "populateListView: Displaying data in the ListView.");
    //get the data and append to a list
    ArrayList<String> taskList = mDatabaseHelper.getTaskList();
    if (adapter == null) {
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.task_name, taskList);
        mListView.setAdapter(adapter);
    } else {
        adapter.clear();
        adapter.addAll(taskList);
        adapter.notifyDataSetChanged();
    }

Im happy to elaborate on any of this if what im asking is unclear and thanks a bunch in advance!

I'm new to java so if Ive made any dumb mistakes please go easy on me!

3
  • 1
    use SimpleCursorAdapter, instead of ArrayAdapter Commented Oct 16, 2018 at 16:29
  • Android studio says this feature is deprecated ad of API 16, is there an alternative? Commented Oct 16, 2018 at 17:38
  • 1
    no, it is not deprecated Commented Oct 16, 2018 at 17:39

2 Answers 2

1

With the stock ArrayAdapter it simply uses the object's toString method to populate the view in the list. There is no scope to pass multiple methods for multiple views.

You could create a custom adapter (subclass ArrayAdapter) or as the data comes from an SQLite database you could utilise a Cursor Adapter such as SimpleCursorAdapter. The latter caters for displaying multiple columns.

The constructor for the SimpleCursorAdapter is SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)

  • Note that there is a similar constructor (less the 6th parameter but that this is deprecated).

The 4th and 5th parameters are used to specify pairs of values (string and int), each pair consisting of the column name and the id of the view (from the layout (2nd parameter)) into which the data from the column will be placed.

  • Note! CursorAdapters require a column name _id and that this column should be an alias of the rowid (e.g. _id INTEGER PRIMARY KEY or _id INTEGER PRIMARY KEY AUTOINCREMENT). If such a column doesn't exist then you could create one in the query by using rowid AS _id as one of the columns e.g. SELECT *, rowid AS _id FROM mytable (note this would not work if the table were defined with WITHOUT ROWID).

    • The _id value (a long 4th parameter) is passed to onItemClick and onItemLongClick methods of the adapter, hence why it is required.
Sign up to request clarification or add additional context in comments.

Comments

0
public ArrayList<String> getTaskList(){
    List<TaskList> taskList = new ArrayList<>();
    SQLiteDatabase td = this.getReadableDatabase();
    Cursor cursor = td.query(TABLE_NAME,new String[]{COL2},null,null,null,null,null);
    while(cursor.moveToNext()){
        int index = cursor.getColumnIndex(COL2);
        int desc = cursor.getColumnIndex(COL4);
       taskList.add(cursor.getString(index),cursor.getString(desc));
    }
    cursor.close();
    td.close();
    return taskList;
}

private void populateListView() {
ArrayAdapter<TaskList> adapter;
List<TaskList> taskList = new ArrayList<>();
//get the data and append to a list
 taskList = mDatabaseHelper.getTaskList();
if (adapter == null) {
    adapter = new ArrayAdapter<TaskList>(this, R.layout.list_item,taskList);
    mListView.setAdapter(adapter);
} else {
    adapter.clear();
    adapter.addAll(taskList);
    adapter.notifyDataSetChanged();
}


public class TaskList {

private String TaskName;
private String TaskDesc;

public TaskList(String taskName, String taskDesc) {
    TaskName = taskName;
    TaskDesc = taskDesc;
}

public String getTaskName() {
    return TaskName;
}

public void setTaskName(String taskName) {
    TaskName = taskName;
}

public String getTaskDesc() {
    return TaskDesc;
}

public void setTaskDesc(String taskDesc) {
    TaskDesc = taskDesc;
}
}

please see the updated methods and also new model class for list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.