Problem with “ViewHolder” class in android
-
19-09-2019 - |
Question
I have a problem with "ViewHolder" class. I use "ViewHolder" to improve my List display speech. I think the code is ok, but it throws exception when I am using "setText" with data from "Cursor". Here is my code:
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)));
Solution
You do not say what the exception is. I am going to guess it is a NullPointerException
, which means either:
- You do not have a widget in your row with
android:id="@+id/title"
, or - You do not have a column in your result set named
SBooksDbAdapter.KEY_TITLE
, or - Somehow you are creating rows with no holder in its tag
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow