2013-08-11 3 views
-1

단순히 배열의 값을 보여주는 대화 상자가 나타나기를 원합니다. 그런 다음 버튼 클릭시 선택한 라디오 버튼의 값을 가져 와서 축배에 표시하고 싶습니다. 미리 답변 해 주셔서 감사합니다! 대단히 감사합니다.안드로이드 문자열 배열에서 값 가져 오기

package com.example.moredialogs; 

import java.lang.reflect.Array; 
import java.util.ArrayList; 

import android.R.string; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.view.Menu; 
import android.widget.Toast; 

public class Next extends Activity { 

private Context mContext; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.next); 

    mContext = this; 


    AlertDialog.Builder builder = 
      new AlertDialog.Builder(mContext); 
     builder.setTitle("Show dialog"); 

     final CharSequence[] choiceList = 
     {"Coke", "Pepsi" , "Sprite" , "Seven Up" }; 

     int selected = 0; 


     builder.setSingleChoiceItems(choiceList, selected, 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface arg0, int arg1) { 
         // TODO Auto-generated method stub 


         // This is where I want the value to be selected 


        } 


     }); 
     builder.setPositiveButton("Sounds Good", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int id) { 
       // User clicked OK, so save the mSelectedItems results somewhere 
       // or return them to the component that opened the dialog 

       // On button click I want the selected Item to go in here 
      } 

     }); 
     builder.show(); 
} 

} 
+1

좋아, 어떤 부분은 당신이 붙어있다? 질문이 뭐야? – codeMagic

+0

나는 onClick 수신기 부분에 갇혀 있는데 배열 choiceList의 값을 가져 와서 표시하는 올바른 방법은 무엇입니까? 나는 자바가 아니라 XML을 통해 그것을하고 싶다. 고맙습니다. – user2673161

답변

0

- 필드 mSelected 만들 : 당신의 ChoiceListener에서

private int mSelected; 

mSelected에 대한 설정 값은 : 당신의 ButtonClickListener에서

builder.setSingleChoiceItems(choiceList, selected, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface arg0, int arg1) { 
     mSelected = arg1; 
    } 
}); 

그냥 Toast을 보여 기각

:

builder.setPositiveButton("Sounds Good", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int id) { 
     final CharSequence item = choiceList[mSelected]; 
     Toast.makeText(mContext, item, Toast.LENGTH_LONG).show(); 
     dialog.dismiss(); 
    } 
}); 
+0

고맙습니다.이 작품은 굉장히 좋습니다! – user2673161

0

먼저 필드에 selected을 입력해야합니다. 그런 다음

// This is where I want the value to be selected 

이 될해야합니다

selected = arg1; 

을 다음 리스너에서 : 처음에는

Toast.makeText(
    Next.this, 
    "Selected " + choiceList[selected], 
    Toast.LENGTH_SHORT) 
.show();