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.

Jeongbebs
  • 3,796
  • 6
  • 30
  • 57

2 Answers2

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

ρяσѕρєя K
  • 127,886
  • 50
  • 184
  • 206
  • will look into these. so far option 2 is what I need. Thanks. – Jeongbebs Feb 23 '15 at 02:46
  • @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 – ρяσѕρєя K Feb 23 '15 at 02:49
  • Hi @ρяσѕρєя K, the example displays a data from an array. Is it advisable to convert my `List` to an array or modify the Adapter class to accept a `List`? – Jeongbebs Feb 23 '15 at 03:55
  • @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 – ρяσѕρєя K Feb 23 '15 at 04:11
  • 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! – Jeongbebs Feb 23 '15 at 04:18
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

Community
  • 1
  • 1
Fahim
  • 11,496
  • 4
  • 36
  • 56
  • 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 – Paul Thompson Feb 23 '15 at 02:50