Я создал настраиваемый адаптер списка, который выводит список имен. Я передаю список только ОДИН РАЗ адаптеру настраиваемого массива, который выводит имена на экран нормально. Но если я посмотрю на Logcat, я вижу, что он повторяется снова и снова примерно 4 раза, даже если запрос, который предоставляет данные, возвращает только 3 строки !!

Может ли это быть вызвано классом GetterSetter (ImageItems), который я часто повторно использую в других настраиваемых адаптерах ArrayAdapter?

Я был бы очень рад, если бы кто-нибудь помог мне разобраться в этом! :)

Основной класс:

...

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!
-4
Edmond Tamas 19 Дек 2014 в 14:58

2 ответа

Лучший ответ

Используйте шаблон проектирования 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;
    }
}
0
Haresh Chhelana 19 Дек 2014 в 12:21