1

I am trying to display data from my sqlite database. In my DatabaseHandler.java I have this method:

public List<Item> getAllItemsinList() {
    List<Item> itemList = new ArrayList<Item>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_ITEMS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Item item = new Item();
            item.set_id(Integer.parseInt(cursor.getString(0)));
            item.set_name(cursor.getString(1));
            item.set_description(cursor.getString(2));
            // Adding contact to list
            itemList.add(item);
        } while (cursor.moveToNext());
    }

    // return item list
    return itemList;
}

It returns a List with the type Item.

In my MainActivity.java I have this method:

private void populateListViewUsingList(){
    List<Item> items;
    items = db.getAllItemsinList();
    listView = (ListView) findViewById(R.id.listItems);
    ArrayAdapter<Item> itemArrayAdapter = new ArrayAdapter<Item>(this,android.R.layout.simple_list_item_1, items);
    listView.setAdapter(itemArrayAdapter);
}

The listview is populated, however, objects are being displayed instead of the data the database contains. Example of text being displayed is migueld.rivera.catalogapp.Item@42a97320. What is the correct way of parsing data? Thanks.

4
  • See this question Commented Feb 23, 2015 at 2:11
  • You'll probably want to override the toString() method of Item to have it display correctly Commented Feb 23, 2015 at 2:12
  • My Item class does not a toString method currently. You are saying I should make one? Commented Feb 23, 2015 at 2:21
  • Yep, it's currently using the default implementation Commented Feb 23, 2015 at 2:22

2 Answers 2

2

Example of text being displayed is migueld.rivera.catalogapp.Item@42a97320. What is the correct way of parsing data?

ArrayAdapter take List of objects and call toString method on item return from List.get method. see here getView implementation from :

 T item = getItem(position);
 if (item instanceof CharSequence) {
   text.setText((CharSequence)item);

  } else {
    text.setText(item.toString());
 }

So,

Option 1: override toString method in Item class which return readable string representation of Item object

@Override
public String toString() {
    return String.format(name + "---" + description);
}

Option 2: Create a custom Adapter by extending ArrayAdapter :

Customizing Android ListView Items with Custom ArrayAdapter

Sign up to request clarification or add additional context in comments.

5 Comments

will look into these. so far option 2 is what I need. Thanks.
@MiguelRivera: yes because after override getView method you can call all getter methods from Item class to get values and show in different TextView's
Hi @ρяσѕρєя K, the example displays a data from an array. Is it advisable to convert my List<Item> to an array or modify the Adapter class to accept a List<e>?
@MiguelRivera: No need to convert List to Array, just pass List object to Custom Adapter class and use List object instead of array in custom adapter class
worked like a charm. I've been into cross platform development and I got rusty in native coding. The tutorial was a great help. Thanks!
1

This is happening because you are direclty displaying the object. Try in this manner in your getview method of the adapter

Items item = getItem(position);

textView.setText(item.get_name()+....+item.get_description);

Use appropriate method name what you have declared in your class

Example for custom listview adapter in shown here ListView with custom adapter

1 Comment

She doesn't have an adapter yet. This might need to be expanded to include the fact you'd need to create a custom adapter

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.