9

I have this below code access the ListView item value into string and display it in alert?

ListView shot = getListView();
shot.setOnItemClickListener(this);

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

    String S = arg1.getContext().toString();
    AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

    // set the message to display
    alertbox.setMessage(S).show();    
}
Janusz
  • 187,060
  • 113
  • 301
  • 369
Srikanth Naidu
  • 787
  • 5
  • 16
  • 28
  • 1
    Please put a little bit more effort in asking the questions. I can't fully understand your problem. What are you trying to do? What is working what not? – Janusz Jun 07 '10 at 10:02
  • 1
    Tip: Don't use `arg0`, `arg1`, etc. as argument names. It makes the source code complete unreadable. And one of them is actually the information you are looking for, so if you had used the proper names you would have not needed to ask this question. – RoToRa Jun 07 '10 at 10:18

4 Answers4

18

maybe this example will help you

  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();
    }
  });

https://developer.android.com/reference/android/widget/ListView.html

jayeshkv
  • 2,180
  • 4
  • 24
  • 42
zed_0xff
  • 32,417
  • 7
  • 53
  • 72
12

This gives you the exact value of the item clicked. Check the log

ListView shot = getListView();
shot.setOnItemClickListener(this);

public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

    String val =(String) parent.getItemAtPosition(position);
    System.out.println("Value is "+val); 
}
Vishwesh Shetty
  • 687
  • 9
  • 27
Droido
  • 167
  • 2
  • 10
1

To get the value of your model

adaptor.getItem(position).getCardName();

C Williams
  • 850
  • 12
  • 19
0

Maybe you can try this

String data = (String)shot.getItemAtPosition(arg2);
AlertDialog.Builder adb = new AlertDialog.Builder(arg1.getContext());              
adb.setMessage(data).show();
Udayaditya Barua
  • 1,151
  • 12
  • 26