I have a list of items that I get from the database and display to an Android screen.
I add the items like this:
JSONArray obj = new JSONArray(result);
if ( obj != null )
{
problems.clear();
List<String> list = new ArrayList<String>();
for ( int i = 0; i < obj.length(); i++ )
{
JSONObject o = obj.getJSONObject(i);
Log.d( "Title: " , "" + o.getString("problem_title") );
Log.d( "id: " , "" + o.getString("problem_id") );
problem_title = o.getString("problem_title");
problem_id = o.getString("problem_id");
problems.add( problem_id );
// Log.d( "MyProblemsActivity" , "problem title: " + problem_title );
Log.d( "MyProblemsActivity" , "problem id: " + problem_id );
}
problems.addAll(list);
adapter.notifyDataSetChanged();
}
Ideally I would display the title string to the user, and keep the id of the item tracked, but hidden from the user. Is there a way to do that?
So far I only figured out how to display the id so that when the person clicks the id, I can get that value like this:
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), (( TextView ) view).getText(),
Toast.LENGTH_SHORT).show();
// For now just do something simple like display a responsive message
Log.d( "MyProblemsActivity" , "A choice was made from the list: " + (( TextView ) view).getText() );
}
});
But is there a way to display the item string, but still be able to know what the id was when the item was clicked?
Thanks!
Sure. You could modify your Adapter / List to take some custom Object and
Edit:
http://www.vogella.de/articles/AndroidListView/article.html has a nice example.