我想我有点生气了......但我无法弄清楚。我想显示一个绑定(bind)到 Onclick 事件的自定义警报对话框,其中包含来自数据库的数据列表。
如果我使用一个简单的对话框,一切都可以完美运行,但我想对其进行自定义,以便在警报对话框顶部设置标题、一些 TextView 和按钮(而不是仅显示带有 . setTitle() 方法)。
这是我的代码:
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
infoListButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom Dialog
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog_institutionalinfos, null);
TextView text = (TextView) layout.findViewById(R.id.custom_dialog_text);
text.setText("Hello, this is a custom dialog!");
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
// inflating the custom institutional expandable list layout
LayoutInflater li = (LayoutInflater) thisContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.institutional_info_custom_list, null, false);
alertDialog.setContentView(v);
InstitutionalInfoListView = (ListView) v.findViewById(android.R.id.list);
final MasterDetailArrayAdapter adapter = new MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
alertDialog.show();
}
});
}
});
}
当我单击 infoListButton 时应用程序崩溃,并且在 LogCat 中收到一条消息:
03-16 19:25:22.905: E/AndroidRuntime(5301): FATAL EXCEPTION: main
03-16 19:25:22.905: E/AndroidRuntime(5301): android.util.AndroidRuntimeException:
requestFeature() must be called before adding content
并且在我调用alertDialog.show();的行上出现错误方法。为什么会出现这种情况?如何解决?
我只想向我的 AlertDialog 添加自定义标题、按钮和 TextView 。
如何做到这一点?
编辑:如何自定义标题并在对话框的右上角添加一个按钮以将其与 onclick 事件绑定(bind),该事件将解雇()对话框???
编辑:我使用以下代码创建了一个 MyCustomDialog 类:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog_institutionalinfos);
TextView dialogTitle = (TextView) findViewById(R.id.custom_dialog_text);
dialogTitle.setText("My First Custom Dialog");
}
}
这个 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/custom_dialog_text"
android:layout_width="80dp"
android:layout_height="wrap_content"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"/>
</RelativeLayout>
我实例化对话框的代码:
// custom Dialog
MyCustomDialog dialog = new MyCustomDialog(thisContext);
// inflating the custom institutional expandable list layout
LayoutInflater li = (LayoutInflater) thisContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.institutional_info_custom_list, null, false);
dialog.setContentView(v);
InstitutionalInfoListView = (ListView) v.findViewById(android.R.id.list);
final MasterDetailArrayAdapter adapter = new MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
dialog.show();
但是对话框中没有显示任何文本。我只看到列表:
我错过了什么? 更新:MyCustomDialog 类:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_title);
TextView dialogTitle = (TextView) findViewById(R.id.custom_dialog_text);
dialogTitle.setText("Hello I am a dialog");
}
}
custom_title.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#3FD100"
android:orientation="vertical" >
<TextView
android:id="@+id/custom_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
我实例化 MyCustomDialog 类的代码:
// custom Dialog
MyCustomDialog dialog = new MyCustomDialog(thisContext);
InstitutionalInfoListView = (ListView) dialog.findViewById(R.id.custom_dialog_list);
final MasterDetailArrayAdapter adapter = new
MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
dialog.show();
应用程序因空指针异常而崩溃
InstitutionalInfoListView.setAdapter(适配器);
更新:TextView 位于 ListView 内部,如何继续?
最佳答案
为什么使用AlertDialog
?您可以使用简单的Dialog
,它通常会导致较少的问题,并且更适合自定义布局。
编辑:详细信息
首先创建一个 XML 布局文件,就像创建 Activity 一样。然后创建一个扩展Dialog
的类。假设您的自定义对话框类名为 CustomDialog
。在 public CustomDialog(Context context)
中调用:
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
// find your views and customize them here
编辑:如何创建自定义对话框实例
- 在您的 xml 中,还为 ListView 指定一个自定义 ID:
android:id="@+id/custom_dialog_list"
- 查看下面部分的评论
//自定义对话框 MyCustomDialog 对话框 = new MyCustomDialog(thisContext);
// you DO NOT need to inflate a new view. by dialog.setContentView(v); you *replace* your XML file with another layout.
// inflating the custom institutional expandable list layout
//LayoutInflater li = (LayoutInflater) thisContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View v = li.inflate(R.layout.institutional_info_custom_list, null, false);
//dialog.setContentView(v);
// dont forget to check this line
InstitutionalInfoListView = (ListView) dialog.findViewById(R.id.custom_dialog_list); // set a custom id for your list in the layout
final MasterDetailArrayAdapter adapter = new MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
dialog.show();
关于java - 如何创建一个自定义alertDialog,其中的列表绑定(bind)到onClick事件android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22441753/