在我的应用程序中,我有一个场景,我需要在许多情况下显示带有标题的 listView。
所以我创建了一个 gen_list_layout.xml,其中包含一个 TextView 和一个 ListView。
现在要显示列表,我计划创建一个方法“public static AlertDialog createAlertDialog(String title, String[] items, Activity activity)”,我可以在其中创建相同的 AlertDialog。
public AlertDialog createAlertDialog(Activity act, String title, String[] items) {
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.gen_list_layout,null);
TextView titleTv = (TextView) view.findViewById(R.id.genListTitTv);
titleTv.setText(title);
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.list_item, R.id.genList, items);
ListView listView = (ListView) findViewById(R.id.genList);
listView.setAdapter(ad);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// I WANT THIS TO BE HNDLED IN THE ACTIVITY CALLING
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
}
});
AlertDialog.Builder scrnDlg = new AlertDialog.Builder(act);
scrnDlg.setView(view);
final AlertDialog adg = scrnDlg.create();
return adg;
}
我将在需要列表的任何地方从不同的 Activity 调用 createAlertDialog()。
AlertDialog ad = Components.createAlertDialog(RouteListActivity.this, title, items);
现在如何处理 AlertDialog 中的 ListView 的点击事件?
任何线索,如何实施。
更新 我在实施您的方法后更新了我的 Q 并且我看到了一个异常。无法获取异常的原因。我创建了一个类并添加了 createAlertDialog :
public class ListAlertDialog {
public AlertDialog createAlertDialog(Activity act, String title, String[] items, OnItemClickListener clickListener) {
Log.i("ListAD", "Start to create Alert Dialog");
LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.gen_list_layout, null);
Log.i("ListAD", "Got View :" + view.getId());
TextView titleTv = (TextView) view.findViewById(R.id.genListTitTv);
titleTv.setText(title);
Log.i("ListAD", "Set Title TV : " + titleTv.getId());
ArrayAdapter<String> ad = new ArrayAdapter<String>(act, R.layout.list_item, R.id.genList, items);
Log.i("ListAD", "Created Adapter");
ListView listView = (ListView) view.findViewById(R.id.genList);
listView.setAdapter(ad);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Log.i("ListAD", "Got ListView :" + listView.getId() + " & Set Adapter");
listView.setOnItemClickListener(clickListener);
Log.i("ListAD", "Added Listerner to listView : " + clickListener.getClass());
AlertDialog.Builder scrnDlg = new AlertDialog.Builder(act);
scrnDlg.setView(view);
Log.i("ListAD", "Set View of scrnDlg");
final AlertDialog adg = scrnDlg.create();
Log.i("ListAD", "Created Alert Dialog & returning it : " + adg );
return adg;
}
}
我的 gen_list.layout.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="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/genListTitTv" style="@style/inPageTitleStyle"
android:text="" />
<ListView android:id="@+id/genList" android:layout_width="match_parent" android:layout_height="wrap_content"
android:dividerHeight="0dp" android:divider="@null" />
</LinearLayout>
在我的活动中,我调用为:
ListAlertDialog lad = new ListAlertDialog();
AlertDialog ad = lad.createAlertDialog(Mumbai77Activity.this, "Select", items, clickListener);
Log.i(TAG, "Got the AD in Activity : " + ad + " ABT to Show");
ad.show();
Log.i(TAG, "Shown ad");
我在日志中得到的错误是 NullPointerException :
08-30 12:52:00.247: I/ListAD(366): Start to create Alert Dialog
08-30 12:52:00.257: I/ListAD(366): Got View :-1
08-30 12:52:00.257: I/ListAD(366): Set Title TV : 2131230764
08-30 12:52:00.257: I/ListAD(366): Created Adapter
08-30 12:52:00.257: I/ListAD(366): Got ListView :2131230765 & Set Adapter
08-30 12:52:00.267: I/ListAD(366): Added Listerner to listView : class org.mumbai77.core.Mumbai77Activity$2
08-30 12:52:00.267: I/ListAD(366): Set View of scrnDlg
08-30 12:52:00.267: I/ListAD(366): Created Alert Dialog & returning it : android.app.AlertDialog@44f29668
08-30 12:52:00.267: I/Mumbai77(366): Got the AD in Activity : android.app.AlertDialog@44f29668 ABT to Show
08-30 12:52:00.297: I/Mumbai77(366): Shown ad
08-30 12:52:00.417: D/AndroidRuntime(366): Shutting down VM
08-30 12:52:00.417: W/dalvikvm(366): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-30 12:52:00.437: E/AndroidRuntime(366): FATAL EXCEPTION: main
08-30 12:52:00.437: E/AndroidRuntime(366): java.lang.NullPointerException
08-30 12:52:00.437: E/AndroidRuntime(366): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:353)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.widget.ListView.onMeasure(ListView.java:1109)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.view.View.measure(View.java:8171)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.view.View.measure(View.java:8171)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.view.View.measure(View.java:8171)
08-30 12:52:00.437: E/AndroidRuntime(366): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
................
在查看异常时,我无法得到实际问题,但在查看我添加的日志时,我可以看到 Got View : -1 I beleiveView view = layoutInflater.inflate(R.layout.gen_list_layout, null);
有一些问题。它无法获得视图或类似的东西。
知道 View 可能有什么问题 - 为什么它没有获得布局视图?在布局或 inflate() 中对 b 进行了任何修改?
任何帮助都非常感谢..
谢谢