I declare this adapter:
ArrayAdapter<Problem> adapter;
I also declare this this ArrayList:
ArrayList<Problem> problems = new ArrayList <Problem>( );
My Problem class has only 2 fields: problemId and problemName.
I make a call to remote db and populate the data like this:
for ( int i = 0; i < obj.length(); i++ )
{
JSONObject o = obj.getJSONObject(i);
problem_title = o.getString("problem_title");
problem_id = o.getString("problem_id");
Problem p = new Problem ( );
p.setProblemId(problem_id);
p.setProblemId(problem_title);
problems.add( p );
}
adapter.notifyDataSetChanged();
So the stuff that gets displayed to the screen is object reference, but what I want displayed is the name, and when a user clicks that item, I should also be able to retrieve the id of that item in my listener here:
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() );
Log.d( "MyProblemsActivity" , "position: " + position );
Log.d( "MyProblemsActivity" , "id: " + id );
}
});
Help is much appreciated!!!!
Thanks!
I think just need to Override th toString() method of your Problem object. Something like
This is the default text an ArrayAdapter will use to fill the TextView.