2011-10-18 6 views
11

मैं एंड्रॉइड अलर्टडिअलॉग को देख रहा हूं, और स्ट्रिंग्स की एक सूची जोड़ने के लिए setItems (...) का उपयोग करने के लिए यह काफी आसान है।अलर्टडिअलॉग सूची में कस्टम ऑब्जेक्ट्स; डिस्प्ले स्ट्रिंग और फिर वास्तविक मान कैसे प्राप्त करें?

हालांकि, ज्यादातर मामलों में आप अच्छी स्ट्रिंग्स दिखाने वाली एक सूची चाहते हैं, लेकिन सूची से कुछ चुनते समय आप वास्तविक मान चाहते हैं और स्ट्रिंग नहीं।

मैं यह नहीं ढूंढ पाया कि इसे आसान और अच्छे तरीके से कैसे किया जाए।

टिप्स? =)

final Button Button1 = (Button) findViewById(R.id.Button1); 
Button1.setOnClickListener(new OnClickListener() 
{ 
    @Override 
    public void onClick(View v) 
    { 
     final CharSequence[] items = { "String 1", "String 2", "String 3" }; 
     // INstead of a string array, I want something like: 
     // ArrayList<CustomObject> test = new ArrayList<CustomObject>(myArray); 
     // And the CustomObject has a toString() and also a value. This array should in the best of worlds be the base for the list below =) 

     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
     builder.setTitle(LanguageHandler.GetString("Test")); 
     builder.setItems(items, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 

       // *** I want to get the value here! *** 

       Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
}); 
+0

आप अधिक eloborate कर सकते हैं? आपको क्या चाहिए? – user370305

+0

अरे, क्षमा करें। अब तक इस टिप्पणी को नहीं देखा। खैर, मुझे लगता है कि उपर्युक्त टिप्पणी पर स्पष्ट है (कोड में टिप्पणी है)। मैं कस्टम ऑब्जेक्ट्स को AlertDialog में जोड़ना चाहता हूं, जहां ऑब्जेक्ट टूस्ट्रिंग() - विधि मुद्रित की जानी चाहिए, लेकिन क्लिक करते समय मैं क्लिक किए गए ऑब्जेक्ट को वापस लौटा देना चाहता हूं ... =) – Ted

+0

मेरा उत्तर देखें, आशा है कि अब आप जो चाहते हैं उसे प्राप्त करें .. बस अपनी ऑब्जेक्ट्स का कस्टम एडेप्टर बनाएं और इसे अपने अलर्टडिअलॉग पर सेट करें .. – user370305

उत्तर

38
CharSequence[] items = { "String 1", "String 2", "String 3" }; के बजाय

आप अपनी चेतावनी संवाद में एक Custom Adapter,

कुछ की तरह है, का उपयोग कर सकते

AlertDialog.Builder builder = new AlertDialog.Builder(MyApp.this); 
      builder.setTitle("Select"); 
      builder.setAdapter(adapter, 
        new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, 
           int item) { 
          Toast.makeText(MyApp.this, "You selected: " + items[item],Toast.LENGTH_LONG).show(); 
          dialog.dismiss(); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 

आपका list_row.xml फ़ाइल

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="48px" 
     android:layout_height="48px" 
     android:layout_gravity="left" /> 

    <TextView 
     android:id="@+id/title" 
     android:textColor="#0000FF" 
     android:text="" 
     android:paddingLeft="10dip" 
     android:layout_gravity="center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

और आपका ListAdapter कुछ की तरह,

String[] items = {"airplanes", "animals", "cars", "colors", "flowers", "letters", "monsters", "numbers", "shapes", "smileys", "sports", "stars" }; 

// Instead of String[] items, Here you can also use ArrayList for your custom object.. 

ListAdapter adapter = new ArrayAdapter<String>(
     getApplicationContext(), R.layout.list_row, items) { 

    ViewHolder holder; 
    Drawable icon; 

    class ViewHolder { 
     ImageView icon; 
     TextView title; 
    } 

    public View getView(int position, View convertView, 
      ViewGroup parent) { 
     final LayoutInflater inflater = (LayoutInflater) getApplicationContext() 
       .getSystemService(
         Context.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) { 
      convertView = inflater.inflate(
        R.layout.list_row, null); 

      holder = new ViewHolder(); 
      holder.icon = (ImageView) convertView 
        .findViewById(R.id.icon); 
      holder.title = (TextView) convertView 
        .findViewById(R.id.title); 
      convertView.setTag(holder); 
     } else { 
      // view already defined, retrieve view holder 
      holder = (ViewHolder) convertView.getTag(); 
     }  

     Drawable drawable = getResources().getDrawable(R.drawable.list_icon); //this is an image from the drawables folder 

     holder.title.setText(items[position]); 
     holder.icon.setImageDrawable(drawable); 

     return convertView; 
    } 
}; 
+0

इससे मुझे बहुत मदद मिली! अच्छा! – Jesse

+0

ऑब्जेक्ट 'टाइल' यहां क्या है? – Ahmed

+0

@JaVAndroid - ओह क्षमा करें ..! 'टाइल 'खींचने योग्य था। मैंने जवाब अपडेट किया। – user370305

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