开发者

ListActivity onListItemClick, ListView has zero children

开发者 https://www.devze.com 2023-03-25 19:00 出处:网络
I\'m running into a problem with a ListActivity. 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. T

I'm running into a problem with a ListActivity.

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.

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.

This is the array used to populate the ListView:

ArrayList<String> myArrayList = new ArrayList<String>();

That array list is populated in the code and is declared as follows:

String[] myAnswerArray = new String[4];

The ListView adapter is set in the follow code extract:

    myArrayList.clear();
    myArrayList.addAll(Arrays.asList(myAnswerArray));


    // attach the adapter to the ListView 
    setListAdapter(myStringArrayAdapter);

The MyStringArrayAdapter extends ArrayAdapter, and shown here for completeness:

public class MyStringArrayAdapter extends ArrayAdapter<String> {
    private Typeface font;
    private static final String TAG = "MyStringArrayAdapter";

    public MyStringArrayAdapter(Context context, int textViewResourceId,
            ArrayList<String> 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;
    }
}

Once the problem started showing up, I added code to check for zero children. Here's how I check for that:

@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开发者_Python百科.d(TAG, "No children found in ListView")

        }    

        // etc...

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.

Anyone have any ideas how to resolve this?


Try with ViewHolder... http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

0

精彩评论

暂无评论...
验证码 换一张
取 消