Creating a dialog after onClick in listView [dupli

2019-09-03 14:25发布

This question already has an answer here:

I'm trying to get a dialog out of a listView. I want to let the user see a dialog with 3 font sizes so he can change the font size of the app. To get to this dialog the user has to tap on a button in a listView. I made it like this:

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
}

}

It doesn't show any errors but it crashes when I click on Font Size (the button to get to the dialog).

Thank you for your time!

3条回答
看我几分像从前
2楼-- · 2019-09-03 14:57

Use Activity Context

Replace

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

By

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

No need for the below in onItemClick

   String[] listItems = {"Colour", "Font Size",};

Use the below instead

   String name = (String) parent.getItemAtPosition(position); //get clicked item
   if(name.equals("Font Size"))
   {
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-09-03 15:18

As Raghunandan already commented above the main problem was for context, Update your click listener for listview like this

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();


                }

            }
        });
查看更多
看我几分像从前
4楼-- · 2019-09-03 15:22

You must create builder first. Please replace your onItemClick with:

            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();
            }
        }
    });
查看更多
登录 后发表回答