2011-10-18 5 views
11

私はAndroid AlertDialogを見てきましたが、setItems(...)を使って表示されるストリングのリストを追加するのに十分簡単です。AlertDialogリストのカスタムオブジェクト。表示文字列を取得する方法と実際の値を取得する方法?

しかし、ほとんどの場合、素敵な文字列を表示するリストが必要ですが、リストから何かを選択するときは、文字列ではなく実際の値が必要です。

私はそれを簡単かつ素敵な方法で行う方法を見つけることができませんでした。

ヒント? =)の代わりにCharSequence[] items = { "String 1", "String 2", "String 3" };

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

あなたはもっと耳を傾けることができますか?あなたが欲しいもの? – user370305

+0

ねえ、すみません。今までこのコメントを見ていませんでした。さて、上記のコメント(コード内のコメント)にそのような明確なものがあると思います。私はカスタムオブジェクトをAlertDialogに追加して、オブジェクトtoString()メソッドを印刷する必要がありますが、クリックするとクリックされたオブジェクトを代わりに返すようにします。=) – Ted

+0

私の答えを見て、あなたが望むものを手に入れようと思っています..オブジェクトのカスタムアダプターを作成し、AlertDialogに設定してください。 – user370305

答えて

38

あなたは警告ダイアログで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

関連する問題