Reputation: 55
I am trying to load an Adapter
from a SQLite database and I cannot seem to wrap my head around how this works. I am extremely new to Android and Java programming so forgive if I stumble around some of the obvious.
I understand that to have a "proper" database setup I must have the 3 java files, the Main, DatabaseHandler and the class for whatever my table is, so getting there. I have most of working in some form but I cannot seem to get my ListView to load with the rows in my table.
I only have one table with two columns, ID and Item. The code in the DatabaseHandler to get all the items is shown below:
public List<Item> getAllItems()
{
List<Item> itemList = new ArrayList<Item>();
String selectQuery = "SELECT * FROM " + TABLE_ITEMS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst())
{
do {
Item item = new Item();
item.setID(Integer.parseInt(cursor.getString(0)));
item.setItem(cursor.getString(1));
itemList.add(item);
} while (cursor.moveToNext());
}
return itemList;
}
How do put that into a ListView Adapter
in the main activity? This is what I tried below and it did not work I was getting pointer positions instead:
List itemArray = db.getAllItems();
//Define a new Adapter
ArrayAdapter<List> adapter = new ArrayAdapter<List>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, itemArray);
//Bind the adapter to the listView
itemView.setAdapter(adapter);
How do I load the items in?
Thanks for any help given.
Socaprice
Upvotes: 0
Views: 905
Reputation: 44118
You can use a SimpleCursorAdapter
here. You won't have to parse anything yourself and you won't need a custom adapter.
First adjust your DB method to return a Cursor
:
public Cursor getData() {
String selectQuery = "SELECT * FROM " + TABLE_ITEMS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
return cursor;
}
Next create and set the SimpleCursorAdapter
:
Cursor cursor = db.getData();
String[] from = new String[] { "StringColumn" };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, from, to, 0);
listView.setAdapter(adapter);
Note: Change "StringColumn"
to the DB column name that will be used.
Edit: You can fetch the column names afterwards like this:
String itemColumn = cursor.getColumnName(1);
And then use it for the SimpleCursorAdapter
:
String[] from = new String[] { itemColumn };
Upvotes: 2
Reputation: 4292
Unless you're working with a simple String array, you would have to implement your own adaptation of ArrayAdapter
along with its getView()
method in order to actually use it in an AdapterView
.
This is a great place to start.
But to give you a rough idea on this (and as I don't justify answering an SO question by only giving links) your ArrayAdapter
would work like this:
XML
layout for your ListView
item. Tis coul be a simple TextView
or complex, composite View
.List<Item>
to the adapter and save it as a global variable (or field).ArrayAdapter
's getView()
to bind the TextView
s Button
s you might have in your XML layout defined above with the actual data from your List<Item>
. Android automatically called this method, passing the actual position, so that you could call the relevant item in your list.Upvotes: 0
Reputation: 627
You should write your own ArrayAdapter. Because the normal ArrayAdapter dont know your item, so he can't do anything with it.
See Custom Array Adapter example / tutorial
Upvotes: 0