奇怪的ListView问题,android。重复输出?

时间:2014-12-19 11:58:28

标签: android for-loop android-listview android-arrayadapter

我创建了一个自定义列表适配器,它输出了一个名称列表。我只将ONCE列表传递给自定义数组适配器,它在屏幕上输出名称就好了。但是如果我看一下Logcat,我可以看到它再次循环大约4次,即使提供数据的查询只返回3行!!

这可能是由于我经常在其他自定义ArrayAdapter中重用的GetterSetter类(ImageItems)造成的吗?

如果有人能帮助我解决这个问题,我会很高兴的! :)

主要课程:

...

public void getTastemakers() {

        List<ImageItem> values = datasource.getTopTastemakers();
        for (int i = 0; i < values.size(); i++) {


            TasteMakers ii = new TasteMakers();
            ii.setUser(values.get(i).getUser());
            ii.setUserId(values.get(i).getUserId());
            ii.setPostCount(values.get(i).getPostCount());


            tasteMakersList.add(ii);
            if(tasteMakersList.size() == values.size()){
            addToRecomendedList(); 
        }

        }
    }

    private void addToRecomendedList() {
        // Find the ListView resource.
        ArrayAdapter<TasteMakers> adapt = new RecomendedListAdapter(
                getActivity(), tasteMakersList);
        TasteMakersListView.setAdapter(adapt);
        TasteMakersListView.setOnItemClickListener(this);

    }

...

自定义ArrayAdapter.class:

public class RecomendedListAdapter extends ArrayAdapter<TasteMakers> {
    Context context;
    List<TasteMakers> info;
    private String LOGTAG;
    private SendNotification sender;

    public RecomendedListAdapter(Context context, List<TasteMakers> info) {
        super(context, android.R.id.content, info);
        this.context = context;
        this.info = info;
    }

    @SuppressLint({ "DefaultLocale", "SimpleDateFormat" })
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = vi.inflate(R.layout.recomended_users_rowlayout, null);

        TasteMakers member = info.get(position);
        System.out.println("position: " + position + "," + " Member name: " + member.getUser());

        Button followUserShort = (Button) view
                .findViewById(R.id.followUserQuick);
        followUserShort.setTag(position);

        ImageView userImg = (ImageView) view.findViewById(R.id.userImg);
        TextView name = (TextView) view.findViewById(R.id.userName);
        name.setText(member.getUser());
        TextView postCount = (TextView) view.findViewById(R.id.postCount);
        postCount.setText(member.getPostCount() + " postari");


        return view;
    }

重复输出的Logcat:

12-19 13:46:25.616: I/LOGTAG(5789): Returned Tastemakers 3 rows
12-19 13:46:25.731: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:25.746: W/Settings(5789): Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
12-19 13:46:25.771: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:25.776: I/System.out(5789): position: 1, Member name: Varga Edmond
12-19 13:46:25.791: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:25.816: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:25.826: I/System.out(5789): position: 1, Member name: Varga Edmond
12-19 13:46:25.876: D/libEGL(5789): loaded /system/lib/egl/libEGL_mali.so
12-19 13:46:25.876: D/libEGL(5789): loaded /system/lib/egl/libGLESv1_CM_mali.so
12-19 13:46:25.881: D/libEGL(5789): loaded /system/lib/egl/libGLESv2_mali.so
12-19 13:46:25.886: E/(5789): Device driver API match
12-19 13:46:25.886: E/(5789): Device driver API version: 29
12-19 13:46:25.886: E/(5789): User space API version: 29 
12-19 13:46:25.886: E/(5789): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Tue Jul 22 19:59:34 KST 2014 
12-19 13:46:25.971: D/OpenGLRenderer(5789): Enabling debug mode 0
12-19 13:46:26.011: D/dalvikvm(5789): GC_FOR_ALLOC freed 3131K, 27% free 12029K/16276K, paused 26ms, total 26ms
12-19 13:46:26.021: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:26.041: D/dalvikvm(5789): GC_FOR_ALLOC freed 156K, 20% free 13064K/16276K, paused 16ms, total 16ms
12-19 13:46:26.046: I/System.out(5789): position: 1, Member name: Varga Edmond
12-19 13:46:26.071: D/dalvikvm(5789): GC_FOR_ALLOC freed 233K, 15% free 13979K/16276K, paused 15ms, total 15ms
12-19 13:46:26.181: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:26.196: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:26.201: I/System.out(5789): position: 1, Member name: Varga Edmond
12-19 13:46:26.211: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:26.221: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:26.226: I/System.out(5789): position: 1, Member name: Varga Edmond
12-19 13:46:27.731: V/AsyncHttpResponseHandler(5789): Progress 80 from 75 (107%)
12-19 13:46:27.741: I/LOGTAG(5789): Returned 1 feed rows
12-19 13:46:27.866: I/LOGTAG(5789): Returned 1 feed rows
12-19 13:46:27.891: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:27.901: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:27.906: I/System.out(5789): position: 1, Member name: Varga Edmond
12-19 13:46:27.941: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:27.951: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:27.956: I/System.out(5789): position: 1, Member name: Varga Edmond
12-19 13:46:28.986: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:29.001: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:29.011: I/System.out(5789): position: 1, Member name: Varga Edmond
12-19 13:46:29.066: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:29.076: I/System.out(5789): position: 0, Member name: Reka Benefi
12-19 13:46:29.081: I/System.out(5789): position: 1, Member name: Varga Edmond
12-19 13:48:20.206: I/System.out(5789): paused!

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

为自定义适配器使用ViewHolder设计模式和BaseAdapter:

public class RecomendedListAdapter extends BaseAdapter {

    Context context;
    List<TasteMakers> info;
    private String LOGTAG;
    private SendNotification sender;

    public RecomendedListAdapter(Context context, List<TasteMakers> info) {
        this.context = context;
        this.info = info;
    }

    @Override
    public int getCount() {
        return info.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return info.get(position);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.recomended_users_rowlayout, null);
            holder.followUserShort = (Button) view.findViewById(R.id.followUserQuick);
            holder.userImg = (ImageView) view.findViewById(R.id.userImg);
            holder.name = (TextView) view.findViewById(R.id.userName);
            holder.postCount = (TextView) view.findViewById(R.id.postCount);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        TasteMakers member = info.get(position);
        System.out.println("position: " + position + "," + " Member name: " + member.getUser());

        holder.followUserShort.setTag(position);
        holder.name.setText(member.getUser());
        holder.postCount.setText(member.getPostCount() + " postari");


        return convertView;
    }

    class ViewHolder {
        Button followUserShort;
        ImageView userImg;
        TextView name;
        TextView postCount;
    }
}