问题:
当我第一次加载列表视图时,我的自定义列表项显示重复的数字:
只有在向下滚动并向上滚动后,才会显示正确的信息:
这是我的适配器代码:
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];
}
}
这是我的清单项目:
<?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>
这就是设置适配器的地方:
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();
}
如何在首次加载时正确加载ListView? 任何帮助,将不胜感激!谢谢!