2010-06-07 11 views
8

मेरे पास यह कोड नीचे है सूची सूची आइटम मूल्य को स्ट्रिंग में एक्सेस करें और इसे अलर्ट में प्रदर्शित करें?एंड्रॉइड में क्लिक किए गए सूचीदृश्य आइटम का मूल्य कैसे प्राप्त करें?

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

कृपया प्रश्न पूछने में थोड़ा और प्रयास करें। मैं आपकी समस्या पूरी तरह से समझ नहीं सकता। तुम क्या करने की कोशिश कर रहे हो? क्या काम नहीं कर रहा है? – Janusz

+1

युक्ति: तर्क नाम के रूप में 'arg0',' arg1', आदि का उपयोग न करें। यह स्रोत कोड को अपठनीय बनाता है। और उनमें से एक वास्तव में वह जानकारी है जिसे आप ढूंढ रहे हैं, इसलिए यदि आपने उचित नामों का उपयोग किया था तो आपको इस प्रश्न से पूछने की आवश्यकता नहीं होगी। – RoToRa

उत्तर

16

शायद इस उदाहरण आप

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

हो सकता है कि आप इस

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

यह आप आइटम का सही मूल्य क्लिक किया देता है कोशिश कर सकते हैं मदद मिलेगी। लॉग

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

जो मेरे लिए क्लासकास्ट अपवाद निकालता है !! – bofredo

संबंधित मुद्दे