• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps setOnItemClickListener() not executing... why?

estex198

Lurker
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:
        // Instantiate AsyncTask and execute the download of products_list.xml, then set ListView
        ProductListDownloadTask download = new ProductListDownloadTask();
        download.execute();

        //TODO populate productName, shelfLife, defaultContainer each time selection changes

       productsListView = (ListView)findViewById(R.id.productsListView);
        tv_productName = (TextView)findViewById(R.id.tv_productName); // product name textview

[B]        productsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
                final Product listProduct = (Product) myAdapter.getItemAtPosition(position);
                tv_productName.setText(listProduct.toString()); // Product object toString() returns name
                Toast.makeText(getApplicationContext(), listProduct.toString(), Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "Hello world!", Toast.LENGTH_SHORT).show();

            }
        });[/B]

This code is outside of the onCreate method, but still within the main activity class:
Code:
        // This is called when doInBackground() is finished
        protected void onPostExecute(Void result) {
            //setup our ArrayAdapter using List<Product> and set it to the ListView.
            mAdapter = new ProductsAdapter(MainActivity.this, -1, XMLParser.getProductsFromFile(MainActivity.this));
            productsListView = (ListView)findViewById(R.id.productsListView);
            productsListView.setAdapter(mAdapter);
            Log.i("Products", "adapter size = " + mAdapter.getCount());

        }

And here is the error from logcat:

Code:
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 =
    java.lang.Throwable: addInArray
            at android.view.ViewGroup.addInArray(ViewGroup.java:3786)
            at android.view.ViewGroup.addViewInner(ViewGroup.java:3740)
            at android.view.ViewGroup.addViewInLayout(ViewGroup.java:3687)
            at android.widget.ListView.setupChild(ListView.java:1862)
            at android.widget.ListView.makeAndAddView(ListView.java:1815)
            at android.widget.ListView.fillDown(ListView.java:698)
            at android.widget.ListView.fillFromTop(ListView.java:759)
            at android.widget.ListView.layoutChildren(ListView.java:1645)
            at android.widget.AbsListView.onLayout(AbsListView.java:2149)
            at android.view.View.layout(View.java:15125)
            at android.view.ViewGroup.layout(ViewGroup.java:4862)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160)
            at android.view.View.layout(View.java:15125)
            at android.view.ViewGroup.layout(ViewGroup.java:4862)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
            at android.view.View.layout(View.java:15125)
            at android.view.ViewGroup.layout(ViewGroup.java:4862)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
            at android.view.View.layout(View.java:15125)
            at android.view.ViewGroup.layout(ViewGroup.java:4862)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
            at android.view.View.layout(View.java:15125)
            at android.view.ViewGroup.layout(ViewGroup.java:4862)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
            at android.view.View.layout(View.java:15125)
            at android.view.ViewGroup.layout(ViewGroup.java:4862)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
            at android.view.View.layout(View.java:15125)
            at android.view.ViewGroup.layout(ViewGroup.java:4862)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2323)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
            at android.view.Choreographer.doCallbacks(Choreographer.java:591)
            at android.view.Choreographer.doFrame(Choreographer.java:560)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
            at android.os.Handler.handleCallback(Handler.java:808)
            at android.os.Handler.dispatchMessage(Handler.java:103)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)
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}
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>
03-17 21:47:04.979    4527-4527/com.estex.kc_app D/OpenGLRenderer﹕ finish <0x60561530>
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
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}}
03-17 21:47:51.011    4527-4527/com.estex.kc_app D/ActivityThread﹕ ACT-PAUSE_ACTIVITY handled : 0 / android.os.BinderProxy@41c3c598
03-17 21:47:51.012    4527-4527/com.estex.kc_app D/ActivityThread﹕ ACT-STOP_ACTIVITY_SHOW handled : 0 / android.os.BinderProxy@41c3c598
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
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}
03-17 21:48:02.609    4527-4542/com.estex.kc_app D/dalvikvm﹕ threadid=12: exiting
03-17 21:48:02.609    4527-4542/com.estex.kc_app D/dalvikvm﹕ threadid=12: bye!
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:
@Overridepublic View getView(int pos, View convertView, ViewGroup parent){RelativeLayout row = (RelativeLayout)convertView;
 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);
 }
//Get our View ReferenceRadioButton r = (RadioButton)row.findViewById(R.id.productListItem);

/* Set the relevant text in our RadioButton
 * a.) When a RadioButton is checked we must call notifyDataSetChanged(), so that all views get updated.
 * b.) When a RadioButton is checked we must set a selectedPosition, to keep track of which RadioButton is selected
 * c.) Views are recycled inside ListViews. Therefore, their absolute position changes in the ListView.
 * Therefore, inside ListAdapter#getView(), we must call setTag() on each RadioButton.
 * This allows us to determine the current position of the RadioButton in the list when the RadioButton is clicked.
 * RadioButton#setChecked() must be updated inside getView() for new or pre-existing Views. */
r.setText(getItem(pos).toString());r.setChecked(pos == selectedPosition);r.setTag(pos);r.setOnClickListener(new View.OnClickListener() {
 @Overridepublic void onClick(View view) {selectedPosition = (Integer)view.getTag();notifyDataSetChanged();
 }
 });
 
Last edited:
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
 
Back
Top Bottom