Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid: Handle events of AlertDialog components in Parent Activity
    primarykey
    data
    text
    <p>In my app I have a scene where I need to show listView with title in many situations. </p> <p>So I created a gen_list_layout.xml with a TextView &amp; a ListView in it.</p> <p>Now to show the list, I planned to create a method "public static AlertDialog createAlertDialog(String title, String[] items, Activity activity)" where I can create teh AlertDialog for the same.</p> <pre><code> 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&lt;String&gt; ad = new ArrayAdapter&lt;String&gt;(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&lt;?&gt; arg0, View v, int position, long id) { } }); AlertDialog.Builder scrnDlg = new AlertDialog.Builder(act); scrnDlg.setView(view); final AlertDialog adg = scrnDlg.create(); return adg; } </code></pre> <p>I will call the createAlertDialog(), from different Activity wherever I need the list.</p> <pre><code> AlertDialog ad = Components.createAlertDialog(RouteListActivity.this, title, items); </code></pre> <p>Now how do I handle click events for the ListView that is in AlertDialog ??</p> <p>Any clue, how to implement such.</p> <p><strong>UPDATE</strong> I update my Q after implementing your approach &amp; I see an exception. Can't get teh cause of the exception. I created a class and added createAlertDialog :</p> <pre><code>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&lt;String&gt; ad = new ArrayAdapter&lt;String&gt;(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() + " &amp; 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 &amp; returning it : " + adg ); return adg; } } </code></pre> <p>My gen_list.layout.xml file :</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"&gt; &lt;TextView android:id="@+id/genListTitTv" style="@style/inPageTitleStyle" android:text="" /&gt; &lt;ListView android:id="@+id/genList" android:layout_width="match_parent" android:layout_height="wrap_content" android:dividerHeight="0dp" android:divider="@null" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>In my Activity, I m calling as :</p> <pre><code> 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"); </code></pre> <p>The error that I get in logs is NullPointerException :</p> <pre><code>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 &amp; 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 &amp; 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) ................ </code></pre> <p>On looking at the exception, I can't get the actual problem, but on looking at the logs that I have added I can see that the Got View : -1 I beleive <code>View view = layoutInflater.inflate(R.layout.gen_list_layout, null);</code>has some problems. It is not able to get the view or somethign like that. </p> <p>Any idea what can be th problem with the View - why it is not getting the view of layout ? Any modifications to b done in the layout or the inflate() ?</p> <p>Any help is highly appreciated..</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload