gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:30:15 24 4
gpt4 key购买 nike

我正在尝试从 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
}

}

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

感谢您的宝贵时间!

最佳答案

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


}

}
});

关于java - 在 listView 中 onClick 之后创建一个对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19456660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com