views:

55

answers:

2

I have an activity that extends ListView. I populate my list with the results of a query to the sqlite database. A list element consists of start_time, end_time, client_name and status. These are the 4 bits on info I need to display for each list item.

My question is: Is it possible for me to assign hidden elements to this ListView item?

For example, I want to store the _id field of the database row for that ListView item, so that when I click it I can start a new activity based on data for that database row.

EDIT

The Code I use to fill my list from my Cursor:

String[] columns = new String[] {VisitsAdapter.KEY_CLIENT_FULL_NAME, VisitsAdapter.KEY_STATUS,VisitsAdapter.KEY_CLIENT_START_TIME, VisitsAdapter.KEY_CLIENT_END_TIME};

// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.name_entry,R.id.number_entry,R.id.start_time_display,R.id.end_time_display  };

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(HomeScreen.this, R.layout.list_element, cur, columns, to);

Where R.layout.list_item is the XML for my list item. I come from a PHP/HTML background so what I would typically do here is have a hidden field to hold my _id value that I could then access once the list item was clicked. can I do something similar in android? can I put a hidden _id field in my xml layout?

A: 

You should consider creating a java object which would gather all this information. for instance, you could create a class like "ListItem" and each item would have 5 properties :
start_time
end_time
client_name
status id (and more if you need)

And then, you have 2 options, either you serialize you object to put the whole object in the database, or you keep the structure you already have (I suppose), and each row of your database becomes a ListItem object. Then, when you query your database, you put them all in an ArrayList, and extend ArrayAdapter to make your own adapter. Finally, in the getView method, you call myListItem.getEnd_Time and all the method you need to display your data, and simply don't use the getId, which you can directly use from the adapter when you need the object.

Sephy
Sorry sephy, Im quite new to android and I dont really follow your answer, Ive posted relevant code to show where Im coming from
Kevin Bradshaw
What is that way of treating an answer when people take time to explain stuff to you and you just vote them down. I can't believe it.
Sephy
Hi Sephy, I should point out that it was not I who downgraded your answer. I appreciate your response and the time you took to look at my question. I cant say who downgraded it as I do not know. The same goes for Kuroutadori answer as well.
Kevin Bradshaw
ok, plz accept my apologies then. That's a weird way of going by downgrading people's answer...
Sephy
+1  A: 

You should try to loosen yourself from the web development frame of mind a little bit. There is no need to store such a thing as the id in a hidden field in your view. That would mix your data with your user interface and create a lot of confusion and unnecessary view objects.

If you are using a sqlite database to store your values you should use a CursorAdapter to manager your list items. The CursorAdapter stores the result that you got from the Database and manages how the data is displayed to the user. If you then register an onItemClickListener on your ListView the listener will receive the adapter and the position of the data item whose cell was clicked in the adapter. Now you can use getItem to retrieve a cursor pointing to the database result that was used to create the listitem. Now you can get the id or all other values that are not shown to the user from the database result and attach it to an intent to start the next activity.

Janusz
When you say I should use a CursorAdapter, do you mean as oppossed to the SimpleCursorAdapter in my example code?
Kevin Bradshaw
Thanks for your response. Having searched the docs on list item listeners I have discovered that onListItemClick automatically passes the rowID of the cursor result. (the _id column from the database). I was going about this all wrong (as you pointed out) as I assumed I would have to add it to the list item manually, I dod not realise that android takes care of this automatically.Thanks, Answer accepted.
Kevin Bradshaw