Android: findViewById for ImageView (custom adapter)

I am trying to reinstall ImageView size. I am using a custom adapter.

View row = convertView; if (row == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = inflater.inflate(R.layout.grid_item, null); } thumb = (ImageView) row.findViewById(R.id.bookThumb); 

When I use ImageView thumb = (ImageView) row.findViewById (R.id.bookThumb); inside the user adapter, it works fine, but since it goes through each line, it scrolls very slowly.

So ... I'm trying to get thumb = (ImageView) row.findViewById (R.id.bookThumb); from my main activity and set its size, but I get Null, and so the application closes.

I tried:

 LinearLayout layout = (LinearLayout) findViewById(R.id.gridLayout); ImageView thumbRow = (ImageView)layout.findViewById(R.id.bookThumb); 

Also tried:

 // gv is the gridview (R.id.gridview) ImageView thumbRow = (ImageView)gv.findViewById(R.id.bookThumb); 

Always get null.

Appreciate any help! Feels like I'm missing something stupid :)

+1
android android-gridview
source share
1 answer

I think the problem arises in two places.

First of all, the original problem. The adapter is slow due to a call to findViewById. You use a redesigned view and it’s fine, but you still view the view for a thumbnail. There is a technique unofficially called ViewHolder that basically makes it a more direct call.

Therefore, instead of just using the redesigned view as a place to start filling in the data, also attach some structure to it so that you can pre-select 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 the variable directly, and no movement is performed. It should be much faster.

Also the second problem is NullPointerExcepton.

I think this is an ambiguity of identifiers mixed with the point at which you call findViewById in your main action. You cannot do it well in your mainActivity, especially if there is a scroll, since you have to intercept the call for rendering in the adapter view. Most callbacks are already in the main thread, so two things cannot happen at the same time with this thread.

So, at the point you called findViewById (), the adapter may not have started yet and attached this view to the hierarchy. And if this happened, perhaps he had not finished manipulating him.

I would try to speed up your adapter before approaching the second problem, since I'm not sure that this is possible even safely or accurately, without completely abandoning the use of the type of adapter.

+3
source share

All Articles