当我单击ListView的项目时,我想解除AlertDialog

时间:2013-02-03 08:36:25

标签: android listview alertdialog onitemclick

我是Android应用开发者。 我想显示AlertDialog包含Listview。

当我点击ListView的项目时,我想解雇AlertDialog。但是,(1)代码不起作用。

我该怎么办?

请告诉我一些建议。预先感谢!

final String[] items = new String[101] ;

for(int m = 0 ; m < items.length ; m++)
{
    items[m] = Integer.toString(m) ;  
}

Dialog dialog = new Dialog(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("sample list title "); 

ListView modeList = new ListView(this); 

ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_single_choice, android.R.id.text1, items);

modeList.setAdapter(modeAdapter);
modeList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

modeList.setOnItemClickListener(new OnItemClickListener()
{
        public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
        {
            makeToast("..test   " + position  );  
            // dialog.dismiss(); ----------------------------(1) 

        }
 }); 

builder.setView(modeList);

dialog = builder.create();
dialog.show(); 

1 个答案:

答案 0 :(得分:2)

使用以下:

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle(getText(R.string.title));
        builder.setSingleChoiceItems(list, position, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int position)
            {
                // do something
                dialog.dismiss();
            }
        });