1. In regards to recently combined carrier specific root forums, if your device forum was affected, please see this poll/thread!

setOnItemClickListener() not executing... why?


  1. estex198

    estex198 Member

    I have an application with a listview consisting of child objects of type Product, a custom class I've created. I'm trying to set the text of a TextView object called tv_productName to the name of the object selected in the ListView. Im not sure whats going on but I get nothing whenever i click any of the ListView child objects, not even the 'Hello world!' toast.

    The following code is in my onCreate() method in the main activity:
    Code (Text):
    1.  
    2.         // Instantiate AsyncTask and execute the download of products_list.xml, then set ListView
    3.         ProductListDownloadTask download = new ProductListDownloadTask();
    4.         download.execute();
    5.  
    6.         //TODO populate productName, shelfLife, defaultContainer each time selection changes
    7.  
    8.        productsListView = (ListView)findViewById(R.id.productsListView);
    9.         tv_productName = (TextView)findViewById(R.id.tv_productName); // product name textview
    10.  
    11. [B]        productsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    12.             public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
    13.                 final Product listProduct = (Product) myAdapter.getItemAtPosition(position);
    14.                 tv_productName.setText(listProduct.toString()); // Product object toString() returns name
    15.                 Toast.makeText(getApplicationContext(), listProduct.toString(), Toast.LENGTH_SHORT).show();
    16.                 Toast.makeText(getApplicationContext(), "Hello world!", Toast.LENGTH_SHORT).show();
    17.  
    18.             }
    19.         });[/B]
    20.  
    This code is outside of the onCreate method, but still within the main activity class:
    Code (Text):
    1.  
    2.         // This is called when doInBackground() is finished
    3.         protected void onPostExecute(Void result) {
    4.             //setup our ArrayAdapter using List<Product> and set it to the ListView.
    5.             mAdapter = new ProductsAdapter(MainActivity.this, -1, XMLParser.getProductsFromFile(MainActivity.this));
    6.             productsListView = (ListView)findViewById(R.id.productsListView);
    7.             productsListView.setAdapter(mAdapter);
    8.             Log.i("Products", "adapter size = " + mAdapter.getCount());
    9.  
    10.         }
    11.  
    12.  
    And here is the error from logcat:

    Code (Text):
    1.  
    2. 03-17 21:47:04.960    4527-4527/com.estex.kc_app D/ViewGroup﹕ addInArray been called, this = android.widget.ListView{41c9d4e8 VFED.VC. .F....ID 370,390-688,880 #7f090062 app:id/productsListView}call stack =
    3.     java.lang.Throwable: addInArray
    4.             at android.view.ViewGroup.addInArray(ViewGroup.java:3786)
    5.             at android.view.ViewGroup.addViewInner(ViewGroup.java:3740)
    6.             at android.view.ViewGroup.addViewInLayout(ViewGroup.java:3687)
    7.             at android.widget.ListView.setupChild(ListView.java:1862)
    8.             at android.widget.ListView.makeAndAddView(ListView.java:1815)
    9.             at android.widget.ListView.fillDown(ListView.java:698)
    10.             at android.widget.ListView.fillFromTop(ListView.java:759)
    11.             at android.widget.ListView.layoutChildren(ListView.java:1645)
    12.             at android.widget.AbsListView.onLayout(AbsListView.java:2149)
    13.             at android.view.View.layout(View.java:15125)
    14.             at android.view.ViewGroup.layout(ViewGroup.java:4862)
    15.             at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160)
    16.             at android.view.View.layout(View.java:15125)
    17.             at android.view.ViewGroup.layout(ViewGroup.java:4862)
    18.             at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
    19.             at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
    20.             at android.view.View.layout(View.java:15125)
    21.             at android.view.ViewGroup.layout(ViewGroup.java:4862)
    22.             at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
    23.             at android.view.View.layout(View.java:15125)
    24.             at android.view.ViewGroup.layout(ViewGroup.java:4862)
    25.             at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
    26.             at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
    27.             at android.view.View.layout(View.java:15125)
    28.             at android.view.ViewGroup.layout(ViewGroup.java:4862)
    29.             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
    30.             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
    31.             at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
    32.             at android.view.View.layout(View.java:15125)
    33.             at android.view.ViewGroup.layout(ViewGroup.java:4862)
    34.             at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
    35.             at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
    36.             at android.view.View.layout(View.java:15125)
    37.             at android.view.ViewGroup.layout(ViewGroup.java:4862)
    38.             at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2323)
    39.             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029)
    40.             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
    41.             at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
    42.             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
    43.             at android.view.Choreographer.doCallbacks(Choreographer.java:591)
    44.             at android.view.Choreographer.doFrame(Choreographer.java:560)
    45.             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
    46.             at android.os.Handler.handleCallback(Handler.java:808)
    47.             at android.os.Handler.dispatchMessage(Handler.java:103)
    48.             at android.os.Looper.loop(Looper.java:193)
    49.             at android.app.ActivityThread.main(ActivityThread.java:5292)
    50.             at java.lang.reflect.Method.invokeNative(Native Method)
    51.             at java.lang.reflect.Method.invoke(Method.java:515)
    52.             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
    53.             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    54.             at dalvik.system.NativeStart.main(Native Method)
    55. 03-17 21:47:04.962    4527-4527/com.estex.kc_app D/ListView﹕ mSelectorRect.setEmpty in layoutChildren this=android.widget.ListView{41c9d4e8 VFED.VC. .F....ID 370,390-688,880 #7f090062 app:id/productsListView}
    56. 03-17 21:47:04.969    4527-4527/com.estex.kc_app D/OpenGLRenderer﹕ prepareDirty (0.00, 0.00, 720.00, 1280.00) opaque 1 <0x60561530>
    57. 03-17 21:47:04.979    4527-4527/com.estex.kc_app D/OpenGLRenderer﹕ finish <0x60561530>
    58. 03-17 21:47:04.981    4527-4527/com.estex.kc_app V/InputMethodManager﹕ START INPUT: android.widget.ListView{41c9d4e8 VFED.VC. .F....I. 370,390-688,880 #7f090062 app:id/productsListView} ic=null tba=android.view.inputmethod.EditorInfo@41cfb370 controlFlags=#100
    59. 03-17 21:47:50.987    4527-4527/com.estex.kc_app D/ActivityThread﹕ ACT-AM_ON_PAUSE_CALLED ActivityRecord{41c3cd68 token=android.os.BinderProxy@41c3c598 {com.estex.kc_app/com.estex.kc_app.MainActivity}}
    60. 03-17 21:47:51.011    4527-4527/com.estex.kc_app D/ActivityThread﹕ ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@41c3c598
    61. 03-17 21:47:51.012    4527-4527/com.estex.kc_app D/ActivityThread﹕ ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@41c3c598
    62. 03-17 21:47:51.080    4527-4527/com.estex.kc_app V/InputMethodManager﹕ START INPUT: android.widget.ListView{41c9d4e8 VFED.VC. .F....I. 370,390-688,880 #7f090062 app:id/productsListView} ic=null tba=android.view.inputmethod.EditorInfo@41ce00c0 controlFlags=#100
    63. 03-17 21:47:56.342    4527-4527/com.estex.kc_app D/AbsListView﹕ onWindowFocusChanged: hasWindowFocus=false, this=android.widget.ListView{41c9d4e8 VFED.VC. .F....I. 370,390-688,880 #7f090062 app:id/productsListView}
    64. 03-17 21:48:02.609    4527-4542/com.estex.kc_app D/dalvikvm﹕ threadid=12: exiting
    65. 03-17 21:48:02.609    4527-4542/com.estex.kc_app D/dalvikvm﹕ threadid=12: bye!
    66.  
    I should probably also mention that I am using a custom ArrayAdapter class to generate the view objects, with each object using a radio button Here is that code:
    Code (Text):
    1.  
    2. @Overridepublic View getView(int pos, View convertView, ViewGroup parent){RelativeLayout row = (RelativeLayout)convertView;
    3.  if(row == null){//No recycled View, we have to inflate one.LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);row = (RelativeLayout)inflater.inflate(R.layout.row_product, null);
    4.  }
    5. //Get our View ReferenceRadioButton r = (RadioButton)row.findViewById(R.id.productListItem);
    6.  
    7. /* Set the relevant text in our RadioButton
    8.  * a.) When a RadioButton is checked we must call notifyDataSetChanged(), so that all views get updated.
    9.  * b.) When a RadioButton is checked we must set a selectedPosition, to keep track of which RadioButton is selected
    10.  * c.) Views are recycled inside ListViews. Therefore, their absolute position changes in the ListView.
    11.  * Therefore, inside ListAdapter#getView(), we must call setTag() on each RadioButton.
    12.  * This allows us to determine the current position of the RadioButton in the list when the RadioButton is clicked.
    13.  * RadioButton#setChecked() must be updated inside getView() for new or pre-existing Views. */
    14. r.setText(getItem(pos).toString());r.setChecked(pos == selectedPosition);r.setTag(pos);r.setOnClickListener(new View.OnClickListener() {
    15.  @Overridepublic void onClick(View view) {selectedPosition = (Integer)view.getTag();notifyDataSetChanged();
    16.  }
    17.  });
    18.  

    Advertisement
    Last edited: Mar 17, 2015
    #1
  2. estex198

    estex198 Member

    So I'm starting to see more clearly what is going on here... Apparently there is a conflict of gaining focus between my listview items and the radio buttons inside these items. I'm trying to implement a custom row layout with a single selection radio button

Share This Page