私のアプリには、多くの状況でタイトル付きのlistViewを表示する必要があるシーンがあります。
そこで、TextViewとListViewを含むgen_list_layout.xmlを作成しました。
リストを表示するために、AlertDialogを作成できるメソッド「publicstatic AlertDialog createAlertDialog(String title、String [] items、Activityactivity)」を作成する予定でした。
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;
}
リストが必要な場所ならどこでも、さまざまなアクティビティから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と思われるView view = layoutInflater.inflate(R.layout.gen_list_layout, null);
問題がいくつかあることがわかります。それはそのような見方や何かを得ることができません。
ビューで何が問題になる可能性があるのか、レイアウトのビューが表示されないのはなぜですか?レイアウトまたはinflate()で行われたbへの変更はありますか?
どんな助けでも大歓迎です。
ありがとう