向下滚动时,我的Listview出现重复项

时间:2018-08-08 04:53:37

标签: android listview arraylist duplicates

请,有人可以帮助我吗?我在Java编码方面非常新。我正在使用Android Studio通过复制和粘贴进行许多试验和错误,从而使某些应用程序正常工作。现在,我正在编写代码,以从MySQL检索数据并将其放在Listview上,但是当我向下滚动时会得到重复的数据。我已经阅读了将近一个星期可以解决该问题的许多建议,并在我的代码上对其进行了尝试,但没有任何效果。因此,如果有人可以帮助我,将不胜感激。这是我的代码。

PersonAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

public class PersonAdapter extends ArrayAdapter<Person> {
public PersonAdapter(Context context, int resource,List<Person> objects) {
    super(context, R.layout.item_row, objects);
}

@Override
public View getView(int position,View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    View view;
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_row, null);
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
        Person p = getItem(position);
        TextView textView = (TextView) convertView.findViewById(R.id.textView2);
        textView.setText(p.getId());
        TextView textView2 = (TextView) convertView.findViewById(R.id.textView);
        textView2.setText(p.getName());
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

static class ViewHolder {
    public TextView textView;
    public TextView textView2;
    public TextView textView3;
    public Button addbutt;
    public Button delbutt;

    public ViewHolder(View convertView) {
        textView = (TextView) convertView.findViewById(R.id.textView);
        textView2 = (TextView) convertView.findViewById(R.id.textView2);
        textView3 = (TextView) convertView.findViewById(R.id.textView3);
        addbutt = (Button) convertView.findViewById(R.id.addbutt);
        delbutt = (Button) convertView.findViewById(R.id.delbutt);
    }
}

Person.java

public class Person {
private String name;
private String id;

public Person(String name, String id) {
    this.name = name;
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

1 个答案:

答案 0 :(得分:0)

覆盖这些功能

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public int getViewTypeCount() {

        return getCount();
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }

适配器应该像

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.TextView;

    import java.util.List;

    public class PersonAdapter extends ArrayAdapter<Person> {
    public PersonAdapter(Context context, int resource,List<Person> objects) {
        super(context, R.layout.item_row, objects);
    }

       @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public int getViewTypeCount() {

            return getCount();
        }

        @Override
        public int getItemViewType(int position) {

            return position;
        }

    @Override
    public View getView(int position,View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        View view;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_row, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
            Person p = getItem(position);
            TextView textView = (TextView) convertView.findViewById(R.id.textView2);
            textView.setText(p.getId());
            TextView textView2 = (TextView) convertView.findViewById(R.id.textView);
            textView2.setText(p.getName());
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        return convertView;
    }

    static class ViewHolder {
        public TextView textView;
        public TextView textView2;
        public TextView textView3;
        public Button addbutt;
        public Button delbutt;

        public ViewHolder(View convertView) {
            textView = (TextView) convertView.findViewById(R.id.textView);
            textView2 = (TextView) convertView.findViewById(R.id.textView2);
            textView3 = (TextView) convertView.findViewById(R.id.textView3);
            addbutt = (Button) convertView.findViewById(R.id.addbutt);
            delbutt = (Button) convertView.findViewById(R.id.delbutt);
        }
    }