getView无法显示列表的所有项目 - android

时间:2016-02-06 17:35:06

标签: android getview

我重写了一个ArrayAdapter来显示存储在数据库中的书籍信息。以下是ViewHolderAdapter:

public class ViewHolderAdapter extends ArrayAdapter<Book>
{
    Context context;
    int layoutResourceId;
    ArrayList<Book> books;
    private SparseBooleanArray mSelectedItemsIds;

    public ViewHolderAdapter(Context context, int layoutResourceId, ArrayList<Book> books) {
        super(context, layoutResourceId, books);
        mSelectedItemsIds = new SparseBooleanArray();
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.books = books;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.cart_row, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.cart_row_title);
            holder.authors = (TextView) convertView.findViewById(R.id.cart_row_author);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Book book = books.get(position);

        holder.title.setText(book.getTitle());
        Log.d("book size", books.size() + "");
        Log.d("book title", book.getTitle());
        .
        .
        .
        return convertView;
    }

    public void updateList(ArrayList<Book> shoppingCart)
    {
        books = shoppingCart;
        Log.d("last book", books.get(books.size()-1).getTitle() + "");
        notifyDataSetChanged();
    }

}
}

问题是当我updateList时,列表不会改变。但是在LogCat中:

  

02-06 05:30:43.375 29052-29052 /? D /书的大小:8

     

02-06 05:30:43.375 29052-29052 /? D /书名:早上

     

02-06 05:30:43.375 29052-29052 /? D /书的大小:8

     

02-06 05:30:43.375 29052-29052 /? D /书名:晚上

     

02-06 05:30:43.375 29052-29052 /? D /书的大小:8

     

02-06 05:30:43.375 29052-29052 /? D /书名:很好

     

02-06 05:30:43.376 29052-29052 /? D /书的大小:8

     

02-06 05:30:43.376 29052-29052 /? D /书名:非常好

     

02-06 05:30:43.376 29052-29052 /? D /书的大小:8

     

02-06 05:30:43.376 29052-29052 /? D /书名:nidaye

     

02-06 05:30:43.376 29052-29052 /? D /书的大小:8

     

02-06 05:30:43.376 29052-29052 /? D /书名:尼玛

     

02-06 05:30:43.376 29052-29052 /? D /书的大小:8

     

02-06 05:30:43.376 29052-29052 /? D /书名:dfd

书对象的book size为8,但book title只显示前七本书,不包括我添加的最后一本书。我认为问题可能在于position。但无法弄清楚。

提前致谢。

0 个答案:

没有答案