2016-09-20 8 views
-1

AlertDialogmultipleChoiceItemsのアイテムを1つの変数に格納する方法を教えてください。セパレータ,もあります。リモートサーバーに渡してphp - explode機能を使用して抽出する必要があります。AlertDialog複数のアイテムセパレータを使用して1つの変数に格納

は、ここに私のデモコードです:MainActivity.java

final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl); 
    Button btn = (Button) findViewById(R.id.btn); 
    final TextView tv = (TextView) findViewById(R.id.tv); 

    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // Build an AlertDialog 
      AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

      // String array for alert dialog multi choice items 
      String[] colors = new String[]{ 
        "Red", 
        "Green", 
        "Blue", 
        "Purple", 
        "Olive" 
      }; 

      // Boolean array for initial selected items 
      final boolean[] checkedColors = new boolean[]{ 
        false, // Red 
        true, // Green 
        false, // Blue 
        true, // Purple 
        false // Olive 

      }; 

      // Convert the color array to list 
      final List<String> colorsList = Arrays.asList(colors);` 

builder.setMultiChoiceItems(colors, checkedColors, new DialogInterface.OnMultiChoiceClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which, boolean isChecked) { 

        // Update the current focused item's checked status 
        checkedColors[which] = isChecked; 

        // Get the current focused item 
        String currentItem = colorsList.get(which); 

        // Notify the current action 
        Toast.makeText(getApplicationContext(), 
          currentItem + " " + isChecked, Toast.LENGTH_SHORT).show(); 
       } 
      }); 

私はcurrentItem変数で選択した項目を保存したいです。

だから、サンプル出力は、(Logcatで)このようになります:Red,Green,Blue,Purple

+0

こんにちは@RoCk、私の答えを受け入れてください。 –

+0

こんにちは@RoCk、あなたはあなたの答えを得ましたか? –

答えて

1

あなたのsetMultiChoiceItems onClickイベントにこれを使用することができます:

currentItem = currentItem+colorsList.get(which)+","; 

Toast.makeText(context,"Appended String :  
"+currentItem,Toast.LENGTH_LONG).show(); 

とあなたの変数を宣言:

String currentItem=""; globally 

サーバーに送信するときは、次のようにしてください:

currentItem=currentItem.substring(0,currentItem.length()-1); 

最後に余分な "、"が削除されます。

は編集:あなたのボタンのonClickイベントでのコメントにあなたの質問のために

はこれを使用する:それはあなたのために働いている場合

for(int i=0;i<colors.length;i++) 
    currentItem = currentItem+colors[i]+","; 

    Toast.makeText(context,"Appended String : "+currentItem,Toast.LENGTH_LONG).show(); 
+1

ねえRoCkこれを試して問題がある場合にお知らせください –

+0

私はそれらをすべて選択するためのボタンを提供するのはどうですか、どうすればできますか? – RoCk

関連する問題