Android ListView с пользовательским адаптером дублирует элемент списка

#android #sqlite #listview #android-listview

#Android #sqlite #listview #android-listview

Вопрос:

У меня есть listview, который заполняет данные из SQLite с помощью пользовательского адаптера. Я использую следующий адаптер.

 public View getView(int pos, View child, ViewGroup parent) {
    CustomerHolder mHolder;
    LayoutInflater layoutInflater;


    if (null == child) {

        layoutInflater = (LayoutInflater) cContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        child = layoutInflater.inflate(R.layout.customer_info, null);
        mHolder = new CustomerHolder();
        mHolder.ccid = (TextView) child.findViewById(R.id.ccid);
        mHolder.ccode = (TextView) child.findViewById(R.id.ttv1);
        mHolder.cname = (TextView) child.findViewById(R.id.cname);
        mHolder.cspcode = (TextView) child.findViewById(R.id.cspcode);
        mHolder.ctype = (TextView) child.findViewById(R.id.ctype);

        child.setTag(mHolder);
    } 
    else 
    {
        mHolder = (CustomerHolder) child.getTag();

    }
    mHolder.ccid.setText(cid.get(pos));
    mHolder.ccode.setText(ccode.get(pos));
    mHolder.cname.setText(cname.get(pos));
    mHolder.cspcode.setText(cspcode.get(pos));
    mHolder.ctype.setText(ctype.get(pos));


    return child;

}

public class CustomerHolder {
    TextView ccid;
    TextView ccode;
    TextView cname;
    TextView cspcode;
    TextView ctype;
}
  

Проблема, с которой я сталкиваюсь, заключается в том, что listview заполняет данные, но каждый элемент списка дублируется. Я рассмотрел вопросы в StackOverflow, но ни одно из решений не сработало для меня.

Ответ №1:

Я проверил свои адаптеры, и единственное отличие от вашего кода заключается в том, что я создаю LayoutInflater только один раз, а не при каждом вызове getView, и я делаю это при создании.

 private final LayoutInflater inflater;
(...)

public PropertyListAdapter(Context context) {
    super(context, 0, new ArrayList<PropertyListItem>());
    this.context = context;
    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    (...)
}
  

Во-вторых, мой класс Holder является статическим, как это:

 static class AdViewHolder {
    public AdView adView;
    public ProgressBar progressBar;
}
  

Все остальное такое же, как в вашем коде. Тот же шаблон. Поэтому, если эти изменения не помогут, попробуйте проверить, случайно ли ваш список объектов / элементов не содержит одинаковых объектов.

Ну и третье — поскольку мой держатель статичен:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    AdViewHolder adHolder = null;

    if (convertView == null) {
     (...)

}
  

Я обнуляю его.

Ответ №2:

Этот адаптер в порядке. Вы дублировали данные в базе данных.