Note that there are some explanatory texts on larger screens.

plurals
  1. POListActivity onListItemClick, ListView has zero children
    primarykey
    data
    text
    <p>I'm running into a problem with a ListActivity. </p> <p>The onListItemClick method needs to access the child views of the ListView in order to highlight the correct answer if an incorrect one was chosen. This logic usually works fine. But the problem is that if I go too fast, i.e. just keep banging away indiscriminately at the display as the lists are presented, before too long the ListView will return 0 children in onListItemClick, and the program will crash on the resulting empty view. My debug statements show that when this occurs, the array used to populate the ListView is correctly initialized, containing all four items as expected. </p> <p>Additional info: When the user responds, I'm writing data to a DB inside of an AsyncTask. When I disable this, the problem seems to go away. I'm passing a subclass of the ApplicationContext to it, but no other data is common to the threads. </p> <p>This is the array used to populate the ListView:</p> <pre><code>ArrayList&lt;String&gt; myArrayList = new ArrayList&lt;String&gt;(); </code></pre> <p>That array list is populated in the code and is declared as follows:</p> <pre><code>String[] myAnswerArray = new String[4]; </code></pre> <p>The ListView adapter is set in the follow code extract:</p> <pre><code> myArrayList.clear(); myArrayList.addAll(Arrays.asList(myAnswerArray)); // attach the adapter to the ListView setListAdapter(myStringArrayAdapter); </code></pre> <p>The MyStringArrayAdapter extends ArrayAdapter, and shown here for completeness:</p> <pre><code>public class MyStringArrayAdapter extends ArrayAdapter&lt;String&gt; { private Typeface font; private static final String TAG = "MyStringArrayAdapter"; public MyStringArrayAdapter(Context context, int textViewResourceId, ArrayList&lt;String&gt; answerArray, Typeface font) { super(context, textViewResourceId, answerArray); this.font = font; } @Override public View getView(int position, View view, ViewGroup viewGroup) { View v = super.getView(position, view, viewGroup); ((TextView) v).setTypeface(font); return v; } } </code></pre> <p>Once the problem started showing up, I added code to check for zero children. Here's how I check for that:</p> <pre><code>@Override protected void onListItemClick(ListView listView, View listItemView, int position, long id) { super.onListItemClick(listView, listItemView, position, id); int childCount = listView.getChildCount(); // Workaround on bug raised by monkey exerciser if (childCount == 0) { Log.d(TAG, "No children found in ListView") } // etc... </code></pre> <p>Again, this problem only happens if I bang away rapidly on the touch screen, and also the exerciser monkey has occasionally but not always generated this I've tried waiting for it to be populated, using Thread.sleep in a loop and re-retrieving the child count, but it makes no difference. </p> <p>Anyone have any ideas how to resolve this? </p>
    singulars
    1. This table or related slice is empty.
    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.
 

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