在listView中onClick后创建一个对话框

时间:2013-10-18 18:29:40

标签: java android listview dialog onclick

我正试图从listView中获取一个对话框。我想让用户看到一个包含3种字体大小的对话框,这样他就可以更改应用程序的字体大小。要进入此对话框,用户必须点击listView中的按钮。我这样做了:

package com.reekapps.simplenote;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Dialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Settings extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);
    getActionBar().setTitle("Settings");



    String[] listItems = {"Colour", "Font Size",};
    ListView lv = (ListView) findViewById(R.id.settings_list);

    lv.setAdapter(new ArrayAdapter<String>
    (this, android.R.layout.simple_list_item_1, listItems));

    lv.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View
                view, int position, long id)
        {
            String[] listItems = {"Colour", "Font Size",};
            if(listItems[position].equals("Font Size"))
            {

                AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                builder.setTitle("Choose Font Size");
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }

        }
    });


    // TODO Auto-generated method stub
}

}

它没有显示任何错误,但是当我点击“字体大小”(按钮进入对话框)时它会崩溃。

感谢您的时间!

3 个答案:

答案 0 :(得分:3)

正如 Raghunandan 上面已经评论过的主要问题是上下文,更新你的listview的点击监听器就像这样

lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String[] listItems = { "Colour", "Font Size", };
                if (listItems[position].equals("Font Size")) {


                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            Settings.this );

                        // set title
                        alertDialogBuilder.setTitle("Choose Font Size");

                        // set dialog message
                        alertDialogBuilder
                            .setMessage("Click yes to exit!")
                            .setCancelable(false)
                            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // if this button is clicked, close
                                    // current activity

                                }
                              })
                            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // if this button is clicked, just close
                                    // the dialog box and do nothing
                                    dialog.cancel();
                                }
                            });

                            // create alert dialog
                            AlertDialog alertDialog = alertDialogBuilder.create();

                            // show it
                            alertDialog.show();


                }

            }
        });

答案 1 :(得分:0)

使用活动上下文

替换

  AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

通过

  AlertDialog.Builder builder = new AlertDialog.Builder(Settings.this);

onItemClick

中不需要以下内容
   String[] listItems = {"Colour", "Font Size",};

使用以下代替

   String name = (String) parent.getItemAtPosition(position); //get clicked item
   if(name.equals("Font Size"))
   {

答案 2 :(得分:0)

您必须先创建构建器。请用以下内容替换onItemClick:

            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String[] listItems = { "Colour", "Font Size", };
            if (listItems[position].equals("Font Size")) {

                AlertDialog builder = new AlertDialog.Builder(
                        Settings.this).create();
                builder.setTitle("Choose Font Size");
                builder.show();
            }
        }
    });