Android Developer 사이트에 보면 다음과 같은 체크박스와 라디오버튼을 사용한 single choice dialog 만들기에 대한 페이지가 있습니다(http://docs.androidside.com/docs/guide/topics/ui/dialogs.html).
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
저는 어떤 이미지를 touch했을 때 이같은 single choice dialog가 뜨도록 만들고 싶습니다만, OnItemClick 함수를 만들어 위 코드를 집어넣은 결과 아무 반응이 없었습니다. 왠지 이유는 아직 찾지 못했습니다. 코드를 첨가해 보이면
public class Function1 extends Activity {
Integer[] imageIDs = {
R.drawable.department1,
R.drawable.department2,
R.drawable.department3
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.function);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent,View v, int position, long id){
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
Toast.makeText(getBaseContext(), "department" + (position +1) + " selected",
Toast.LENGTH_SHORT).show();
}
});
}
ImageAdapter 함수는 생략했습니다. 저의 질문은 어느 부분이 잘못되어 이 다이얼로그가 생성되지 않는가 하는 것입니다. 물론 run은 문제가 없습니다. 마지막 부분에 있는 toast는 또 정상적으로 실행이 되구요.
코드상의 문제점을 발견하실 수 있는 고수님들 혹시 지적이 가능하시다면 부탁드립니다. 아니면 이미지를 click했을 때 리스트 중 하나의 아이템을
선택해서 그 아이템이 toast에 나타나게끔 하는 다이얼로그를 만들어 주실 수 있으신지요?
다이얼로그의 경우 보통 세가지 작업을 해야 동작을 하는데요 (여러 다이얼로그를 분기가 가능하게 하려면)
우슨 AlertDialog 를 필요한만큼 Builder 을 통해서 생성 하시고,
onCreateDialog 에서는 이벤트 소스에 따라 분기를 하셔서, 상황에 맞는 다이얼로그를 return 해주시면 됩니다.
마지막으로 실제적으로 이벤트를 호출하는 부분은
showDialog() 에 인자로 onCreateDialog 에서 분기가 가능한 인자값을 넣어주시면 됩니다.
private int REFRESH_DIALOG = 1004;
....
refreshBtn.setOnClickListener(new OnClickListener
{
public void onClick(View view)
{
showDialog(REFRESH_DIALOG);
}
}
protected Dialog onCreateDialog(int dialog)
{
switch(dialog)
{
case REFRESH_DIALOG :
return refreshDialog();
default :
return super.onCreateDialog(dialog);
}
}
private AlertDialog refreshDialog()
{
다이얼로그 정의 부분...
}
대략 이렇게 되겠습니다.
return refreshDialog;
Showing Dialog부분을 참조하시면 될것 같네요.
onCreateDialog에서 다이얼로그를 만드시고, 해당부분에는 showDialog()를 이용해서 다이얼로그를 보여주시면 됩니다.