2010-06-07 37 views
8

Tengo este código a continuación para acceder al valor del elemento ListView en una cadena y mostrarlo en alerta?¿Cómo obtener el valor de un elemento de Listview en el que se hace clic en Android?

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

Por favor, haga un poco más de esfuerzo para hacer las preguntas. No puedo entender completamente tu problema. ¿Que estás tratando de hacer? ¿Qué está funcionando qué no? – Janusz

+1

Consejo: No use 'arg0',' arg1', etc. como nombres de argumento. Hace que el código fuente sea ilegible. Y uno de ellos es en realidad la información que está buscando, por lo que si hubiera usado los nombres propios, no habría necesitado hacer esta pregunta. – RoToRa

Respuesta

16

tal vez este ejemplo le ayudará a

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

0

Tal vez usted puede probar este

String data = (String)shot.getItemAtPosition(arg2); 
AlertDialog.Builder adb = new AlertDialog.Builder(arg1.getContext());    
adb.setMessage(data).show(); 
10

Esto le da el valor exacto del tema se hace clic. Compruebe el registro

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); 
} 
+0

que dispara una ClassCastException para mí !! – bofredo

Cuestiones relacionadas