android - android中 "ViewHolder"类的问题

标签 android list android-cursor android-viewholder

我对“ViewHolder”类有疑问。我使用“ViewHolder”来改进我的列表显示语音。我认为代码没问题,但它会抛出异常 我正在将“setText”与“Cursor”中的数据一起使用。这是我的代码:

if(row==null){
            LayoutInflater inflater = LayoutInflater.from(context);         
            row = inflater.inflate(R.layout.sbooks_row, null);
            holder = new ViewHolder();

            holder.id = (TextView)row.findViewById(R.id.id);
            holder.title = (TextView)row.findViewById(R.id.title);
            holder.icon = (ImageView)row.findViewById(R.id.icon);

            row.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)row.getTag();
        }

        holder.title.setText(cursor.getString(cursor.getColumnIndex(SBooksDbAdapter.KEY_TITLE)));
        holder.id.setText(cursor.getString(cursor.getColumnIndex(SBooksDbAdapter.KEY_ROWID)));

最佳答案

你没有说异常(exception)是什么。我猜测这是一个 NullPointerException,这意味着:

  1. 您的行中没有 android:id="@+id/title" 的小部件,或者
  2. 您的结果集中没有名为 SBooksDbAdapter.KEY_TITLE 的列,或者
  3. 不知何故,您创建的行的标签中没有持有人

关于android - android中 "ViewHolder"类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1332271/

相关文章:

android - 使用 Android 和自签名证书

android - 在Android中,Task == Application?

c++ - 搜索列表中的最后一个数据实例

android - 使用自定义 simpleCursorAdapter

android - 无法恢复 Activity : trying to requery an already closed cursor

android - 人像图片上传到服务器后旋转90度

android - 在 Xamarin.Android 中通过 MVVMCross 绑定(bind) OxyPlot

c# - 只读字符串列表

list - Haskell - 数字和列表

android - 游标在调用此方法之前停用