我对“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
,这意味着:
- 您的行中没有
android:id="@+id/title"
的小部件,或者 - 您的结果集中没有名为
SBooksDbAdapter.KEY_TITLE
的列,或者 - 不知何故,您创建的行的标签中没有持有人
关于android - android中 "ViewHolder"类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1332271/