Android: FindViewById Of An ImageView (custom Adapter)
Solution 1:
I think the problem stems in two places.
First off the original problem. Your Adapter is slow because of the findViewById call. You make use of the recycled view and that's great, but you still traverse the view for the thumbnail. There is a technique, informally called ViewHolder, which basically makes it more of a direct call.
So instead of just using the recycled view as a place to start filling data, also attach some structure to it so you can prefetch the elements you want to manipulate
if (row == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.grid_item, null);
row.setTag(row.findViewById(R.id.bookThumb));
}
thumb = (ImageView) row.getTag();
Here we call directly by variable and no traversing is done. This should be much faster.
Also the second problem, the NullPointerExcepton.
I think it's the ambiguity of ids mixed with the point at which you call findViewById in your main activity. You can't really do it well in your mainActivity especially if there is scrolling involved as you would have to intercept the call to render within the adapter view. Most of the callbacks are already on the main thread so two things cannot happen at the same time on that thread.
So at the point you called findViewById() the adapter may not have even run yet and attached that view to the hierarchy. And if it did, it might not have finished manipulating it yet.
I would try to speed up your adapter before approaching the second issue as I am not sure it is even possible to do it safely or accurately, without abandoning the use of an adapter view entirely.
Post a Comment for "Android: FindViewById Of An ImageView (custom Adapter)"