0

I have problem with displaying data from db to listview. Here is code:

DB class:

public ArrayList<Task> getTasks(){
    ArrayList<Task> tasks = new ArrayList<Task>();
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor=db.query(TABLE_TASKS, null, null, null, null, null, null);


    if(cursor.moveToFirst()){
        do{
            Task task = new Task(
                    cursor.getString(1),
                    cursor.getString(3),
                    cursor.getString(2));
            tasks.add(task);
        }while(cursor.moveToNext());
    }
    db.close();
    return tasks;
}

Task class

 public Task(String name, String description, String priority){
    super();
    this.name = name;
    this.description = description;
    this.priority = priority;
}

List activity class

 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    tasksListView = (ListView) findViewById(R.id.tasksListView);
    myTasks = dbHelper.getTasks();// metoda isto vraća array list

    myAdapter = new ArrayAdapter<Task>(this, android.R.layout.simple_list_item_1, myTasks);

    tasksListView.setAdapter(myAdapter);

}

I get: Results sreenshot

MyAdapter values

Adapter values from debuger

How can i display values from myadapter to listview?

0

2 Answers 2

1

The adapter has no way to understand how to display your Task data so you should override toString method of your class which may solve your problem:

public Task(String name, String description, String priority){
    super();
    this.name = name;
    this.description = description;
    this.priority = priority;

    public String toString()
    {
        return "name: "+name+" description: "+description+" priority: "+ priority;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Quoting the official documentation of ArrayAdapter :

However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.

So, you know what to do. Override the ToString() method to something like :

public String toString()
{
    return "name: "+name;
}

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.