我试图在我的应用中ListView
中实施AlertDialog
。
点击按钮后调用listDialog()
:
public void listDialog() {
LayoutInflater shoppingListInflater = LayoutInflater.from(this);
final View allListsView = shoppingListInflater.inflate(R.layout.list_picker_dialog, null);
listsPickerView = (ListView) findViewById(R.id.shopping_lists_all);
shoppingLists = new ArrayList<ShoppingList>();
allListsAdapter = new ShoppingListsAdapter(this, shoppingLists);
listsPickerView.setAdapter(allListsAdapter);
AlertDialog.Builder listsDialog = new AlertDialog.Builder(this);
listsDialog.setView(allListsView);
listsDialog.setCancelable(false)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertD = listsDialog.create();
alertD.show();
}
在我的活动中,我有:
private ListView listsPickerView;
private ShoppingListsAdapter allListsAdapter;
private List<ShoppingList> shoppingLists;
我的适配器看起来像这样:
public class ShoppingListsAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<ShoppingList> shoppingLists;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public ShoppingListsAdapter(Activity activity, List<ShoppingList> shoppingLists) {
this.activity = activity;
this.shoppingLists = shoppingLists;
}
@Override
public int getCount() {
return shoppingLists.size();
}
@Override
public Object getItem(int location) {
return shoppingLists.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.shopping_list, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
TextView name = (TextView) convertView
.findViewById(R.id.listName);
TextView listCount = (TextView) convertView
.findViewById(R.id.listCount);
TextView list_id = (TextView) convertView
.findViewById(R.id.list_id);
ShoppingList sList = shoppingLists.get(position);
// set list count
listCount.setText(sList.getListCount());
//set name
name.setText(sList.getName());
list_id.setText(String.valueOf(sList.getId()));
return convertView;
}
}
我的list_picker_dialog
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff">
<ListView
android:id="@+id/shopping_lists_all"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:dividerHeight="1dp"
android:cacheColorHint="#00000000"/>
</LinearLayout>
我在这一行获得了一个NPE:listsPickerView.setAdapter(allListsAdapter);
方法listsDialog()
。我在Dialog中实现ListView有什么问题吗?提前谢谢。
答案 0 :(得分:1)
由于您的ListView
可能位于AlertDialog
的布局中,因此您需要在View
为Dialog
充气。像这样:
listsPickerView = (ListView) allListsView.findViewById(R.id.shopping_lists_all);
您获得NullPointerException
的原因是因为您编写的代码正在ListView
的{{1}}布局中搜索Activity
,当它找不到时返回null
。