ListView复制Android [英] ListView Duplicates Android

查看:149
本文介绍了ListView复制Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从表格中获得搜索结果时,结果显示两次.

When I get a search result from my table, the results show up twice.

结果"a/a","aa/aa"和"ab/a"都是正确的,应该存在.但是,我不希望在第三个listview条目中有重复的值.

The results "a / a", "aa / aa", and "ab / a" are all correct and should be there. However, I don't want the duplicate values in the third listview entry.

有任何想法为什么会发生这种情况?

Any ideas why this is happening?

主屏幕

// Set up search array
    for(int i = 0; i < isbn.length; i++)
    {
        searchArray.add(new InventoryItem(isbn[i], InventoryAdapter.getTitleAndAuthorByISBN(isbn[i])));
    }
    Toast.makeText(getApplicationContext(), "searchArray.size()="+searchArray.size(), Toast.LENGTH_LONG).show();

    // add data in custom adapter
    adapter = new CustomAdapter(this, R.layout.list, searchArray);
    ListView dataList = (ListView) findViewById(R.id.list);
    dataList.setAdapter(adapter);

CustomAdapter

public class CustomAdapter extends ArrayAdapter<InventoryItem> {
   Context context;
   int layoutResourceId;
   LinearLayout linearMain;
   ArrayList<InventoryItem> data = new ArrayList<InventoryItem>();

   public CustomAdapter(Context context, int layoutResourceId,
                 ArrayList<InventoryItem> data) {
          super(context, layoutResourceId, data);
          this.layoutResourceId = layoutResourceId;
          this.context = context;
          this.data = data;
   }

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

          if (row == null) {
                 LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                 row = inflater.inflate(layoutResourceId, parent, false);
                 linearMain = (LinearLayout) row.findViewById(R.id.lineraMain);
                 }

                 InventoryItem myItem = data.get(position);
                 TextView label = new TextView(context);
                 label.setText(myItem.details);
                 linearMain.addView(label);

          return row;

   }
}

推荐答案

好,问题出在您的getView(int position, View convertView, ViewGroup parent)方法中,因为您一直在将视图添加到listview为您提供的先前回收的视图中.除了动态创建textview之外,您还应该在创建视图时对其进行充气.

Well, the problem is in your getView(int position, View convertView, ViewGroup parent) method, because you keep adding views to the previously recycled view that listview provides you. Instead of creating a textview dynamically, you should inflate it when you create your view.

例如,要解决此问题:

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

          if (convertView == null) {
                 LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                 row = inflater.inflate(layoutResourceId, parent, false);
                 //Make sure the textview exists in this xml
          } else {
                 row = convertView;
          }

          InventoryItem myItem = data.get(position);
          TextView label = (Textview) row.findViewById(R.id.YOU_TEXT_VIEW_ID);
          label.setText(myItem.details);

          return row;

   }

这篇关于ListView复制Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆