Skip to content Skip to sidebar Skip to footer

Android Listview - Called Too Many Times Or Changes Order.. How To Prevent Both?

From the Android development page we have the following quote for Custom ArrayAdapter of ListViews: 'there is absolutely no guarantee on the order in which getView() will be calle

Solution 1:

May I suggest you change your code a little. My ListView's getView() always looks like this and I have no problems with it.

@OverridepublicvoidgetView(int position, View convertView, ViewGroup parent) {

    Viewview= convertView;
    MyViewHolderholder=newMyViewHolder();

    if (view == null) {

        view = inflater.inflate(layoutResourceId, null);

        holder.textView1 = (TextView) view.findViewById(R.id.my_tv1);
        holder.button1 = (Button) view.findViewById(R.id.my_btn1);
        // More views

        view.setTag(holder);
    } else {
        holder = (MyViewHolder) view.getTag();
    }

    holder.textView1.setText("Some text");
    holder.button1.setText("Some button text");
    holder.button1.setEnabled(buttonEnabled);
}

publicstaticclassMyViewHolder {
    public TextView textView1;
    public Button button1;
    // ... and so on
}

Solution 2:

That isn't a bug!

It doesn't matter how many times getView() called. Every time that android want show a row in your Listview call this method.

You can override getItem(int position) of Adapter for return every Object you want in every position. Besides of this check your list_item height.

@OverridepublicObjectgetItem(int position) {
        Object item = null;
        if (position >= 0 && position < mData.size()) {
            item = mData.get(position);
        }
        else {
            Log.e(TAG, "Bad position in getItem");
        }

        return item;
    }

mData is your ArrayList of your data that bound to listview.

And be careful that in getView() you should set values for all of your views. that is important.

Solution 3:

Why do you save OrderedProductItem object in class ListItemTagHolder ? you always get data with this command

getItem(position)

and you don't need to save this object in your ListItemTagHolder class. Maybe this command

h.orderedProductItem = Controller.getInstance().getOrderedProductItems().get(position);

return wrong data!! I think if you change above command and move out out of ListItemTagHolder class, your problem will be fixed. :)

Post a Comment for "Android Listview - Called Too Many Times Or Changes Order.. How To Prevent Both?"