在listView中的onClick之后创建对话框 [英] Creating a dialog after onClick in listView

查看:53
本文介绍了在listView中的onClick之后创建对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从listView中获取对话框.我想让用户看到具有3种字体大小的对话框,以便他可以更改应用程序的字体大小.要进入此对话框,用户必须在listView中点击一个按钮.我是这样的:

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).

谢谢您的时间!

推荐答案

正如上面的 Raghunandan 所说的那样,主要问题是关于上下文的问题,像这样为listview更新您的点击侦听器

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


                }

            }
        });

这篇关于在listView中的onClick之后创建对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆