Java PopupDialog中的ListView为空
在我的应用程序中,我试图在Java PopupDialog中的ListView为空,java,android,Java,Android,在我的应用程序中,我试图在弹出对话框中使用列表视图。这是在异步任务中完成的。我认为问题出在onPreExecute和onPostExecute之间……一切都正常返回,但在我使用findViewById()之后,list=null。我不明白为什么……注意,AsyncTask和Adapter都是内部类 protected void onPreExecute() { AlertDialog.Builder alert = new AlertDialog.Builder(Crea
弹出对话框
中使用列表视图
。这是在异步任务
中完成的。我认为问题出在onPreExecute
和onPostExecute
之间……一切都正常返回,但在我使用findViewById()之后,list=null
。我不明白为什么……注意,AsyncTask和Adapter都是内部类
protected void onPreExecute() {
AlertDialog.Builder alert = new AlertDialog.Builder(CreateNewReport.this);
LayoutInflater inflater = LayoutInflater.from(CreateNewReport.this);
popupView = inflater.inflate(R.layout.add_expenses, null);
alert.setTitle("Add Expenses").setView(popupView);
alert.setNeutralButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
protected void onPostExecute(List<Expense> expenses){
availableExpenses = expenses;
// create list adapter for available expenses
ArrayAdapter<Expense> adapter = new ArrayAdapter<Expense>(CreateNewReport.this,
android.R.layout.simple_list_item_1, expenses);
// get a reference to the list
final ListView list = (ListView) findViewById(R.id.listViewAvailableExpenses);
// set the list adapter
list.setAdapter(adapter);
// find widgets
final ProgressBar progress = (ProgressBar) findViewById(R.id.loading_expenses);
final LinearLayout listLayout = (LinearLayout) findViewById(R.id.available_expenses);
final LinearLayout progressContainer = (LinearLayout) findViewById(R.id.available_expenses_loading);
// change visibility as needed
progressContainer.setVisibility(View.INVISIBLE);
progress.setVisibility(View.INVISIBLE);
listLayout.setVisibility(View.VISIBLE);
}
受保护的void onPreExecute(){
AlertDialog.Builder alert=新建AlertDialog.Builder(CreateNewReport.this);
LayoutInflater充气器=LayoutInflater.from(CreateNewReport.this);
popupView=充气机。充气(R.layout.add\u费用,空);
alert.setTitle(“添加费用”).setView(popupView);
alert.setNeutralButton(“保存”,新建DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
}
});
alert.show();
}
执行后受保护的作废(列出费用){
可用费用=费用;
//为可用费用创建列表适配器
ArrayAdapter=新的ArrayAdapter(CreateNewReport.this,
android.R.layout.simple_list_item_1,费用);
//获取对列表的引用
最终ListView列表=(ListView)findViewById(R.id.listViewAvailableExpenses);
//设置列表适配器
list.setAdapter(适配器);
//查找小部件
最终ProgressBar进度=(ProgressBar)findViewById(R.id.装载费用);
最终线性布局列表布局=(线性布局)findViewById(R.id.可用费用);
最终线性布局进度容器=(线性布局)findViewById(R.id.可用费用\装载);
//根据需要更改可见性
progressContainer.setVisibility(View.INVISIBLE);
progress.setVisibility(View.INVISIBLE);
listLayout.setVisibility(View.VISIBLE);
}
弹出框的XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="@+id/loading_expenses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Large"
android:layout_centerInParent="true"
/>
<ListView
android:id="@+id/listViewAvailableExpenses"
style="@style/Container"
android:cacheColorHint="#00000000"
/>
</RelativeLayout>
这是因为findViewById()
调用正在活动中查找您的列表(这是活动实例的一种方法),但您的列表在对话框中。相反,您需要保存对该对话框的引用,然后执行dialog.findViewById(R.id.ListNote)
:
//PreExecute
theDialog = alert.create(); //theDialog is an instance variable of the asynctask
theDialog.show();
//PostExecute
final ListView list = (ListView) theDialog.findViewById(R.id.ListNote);