-1

I am using an XML (HttpPost) on ListView to display a list of items in my application. XML File passes the following sample data:

  id=5
  name=Name2
  score=20

The problem I am facing is with the OnItemClick:

public void onItemClick(AdapterView parent, View view, int position, long id) { lv.getItemAtPosition(position);

            AlertDialog.Builder alert = new AlertDialog.Builder(context);

            alert.setTitle("Selected Name: " + lv.getItemAtPosition(position));

Here lv is the listview. The out put shows:

Selected Name: {id=5, name=Name2, score=20}

What should I be doing if I need an output like:

Selected Name: Name2

Thanks Ram

Ramanathan
  • 1,663
  • 4
  • 17
  • 24

3 Answers3

0

getItemAtPosition returns an Object class object You must to cast it to your type:

alert.setTitle("Selected Name: " + ((YourClass)lv.getItemAtPosition(position)).getName());

Or owerride toString() method in your class:

@Owerride
String toString(){
    return name;
}
Roman Black
  • 3,501
  • 1
  • 22
  • 31
  • this is assuming he had a getName() method but of course he should be doing that anyway. That is good practice/design. – JoxTraex Jan 22 '12 at 04:12
0

Another option is you take the String, then tokenize it or something based on the first "," then take the first token and throw it into the Title of the Dialog.

Refer: StringTokenizer class

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • Thanks but there should be an easier way out to do this. I will do some more research. Thanks. – Ramanathan Jan 22 '12 at 02:23
  • I used the same variable which I used to build the listview from XML and it worked. Should have thot of it earlier. Thanks for all your help. – Ramanathan Jan 22 '12 at 07:33
0

I used the same variable which I used to build the listview from XML and it worked. Should have thot of it earlier. Thanks for all your help.

Ramanathan
  • 1,663
  • 4
  • 17
  • 24