1

Problem:

When I load the list view for the first time, my custom list items show a repeated number: ListView at First

Only after scrolling down and scrolling up, does the correct information appear: ListView after Scrolling

This is my adapter code:

public class HistoryAdapter extends ArrayAdapter<Integer> {

    Context mContext;
    int mLayoutResource;
    Integer[] mObjects;
    int count;

    public HistoryAdapter(Context context, int resource, Integer[] objects) {
        super(context, resource, objects);
        mContext = context;
        mLayoutResource = resource;
        mObjects = objects;
        count = mObjects.length;
        for (int iii = mObjects.length - 1; objects[iii] == null; iii--) {
            count--;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            int data = mObjects[count - position - 1];
            if (convertView == null) {
                LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
                convertView = inflater.inflate(mLayoutResource, parent, false);
            }
            CardView cardView = (CardView) convertView.findViewById(R.id.cardView);
            CircleView2 circleView = (CircleView2) cardView.findViewById(R.id.circleView);
            int parentHeight = cardView.getHeight();
            int circleHeight = cardView.getHeight();
            int circleWidth = cardView.getHeight();
            int margin = (parentHeight - circleHeight) / 2;
            circleView.layout(margin, margin, margin + circleWidth, margin + circleHeight);
            circleView.setStepsString(data);
            circleView.invalidate();
            return convertView;
        } catch (NullPointerException e) {
            return null;
        }
    }

    @Override
    public int getCount() {
        Log.d("HistoryAdapter", "getCount");
        return count;
    }

    @Override
    public Integer getItem(int position) {
        return mObjects[position];
    }
}

This is my list item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listItem"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <android.support.v7.widget.CardView
        xmlns:card_view = "http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardView"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        card_view:cardCornerRadius="10dp"
        card_view:cardElevation="20dp">

            <com.smartfitness.daniellee.fittracker.CircleView2
                android:id="@+id/circleView"
                android:layout_width="150dp"
                android:layout_height="150dp"
                />
        </android.support.v7.widget.CardView>


</LinearLayout>

And this is where the Adapter is set:

protected void onPostExecute(DataReadResult dataReadResult) {
            List<Bucket> buckets;
            buckets = dataReadResult.getBuckets();
            days = new Integer[buckets.size()];
            for (int iii = 0; iii < buckets.size(); iii++) {
                dumpDataSet(buckets.get(iii).getDataSet(DataType.AGGREGATE_STEP_COUNT_DELTA));
            }
            HistoryAdapter adapter = new HistoryAdapter(getActivity(), R.layout.list_item, days);
            listView.setAdapter(adapter);
            mProgress.dismiss();
        }

How can I make the ListView load correctly when it is first loaded? Any help would be appreciated! Thanks!

dan.lee
  • 695
  • 1
  • 5
  • 9

0 Answers0