Tuesday, July 29, 2014

About nuances

It's been a while since I have written here.

My application is ready, except design.

I have used a lot of features, and learned even more. May be I have to write time to time something here about. But this is not a habit. Yet.

Last thing I want to add to my app is App Navigation. And this is much simpler, that asynchronous queries, multichoice action mode and other twists and turns. One of them I have found just now.

If you use ArrayAdapter, good practice is to reuse "item view" instead of inflating it every time:

 public View getView(int position, View convertView, ViewGroup parent) {  
   //item from List in this particular View  
   final Component component = getItem(position);   
   //reuse view, inflate only if null
   if (convertView == null) {        
     convertView = View.inflate(getContext(), R.layout.list_item_component, null);        
   }  
   //this button call Dialog to choose Food  
   Button foodButton = (Button) convertView.findViewById(R.id.component_button);  
   //if component is not empty, than show Food name on button  
   //otherwise foodButton will have default caption, defined in layout xml  
   if (component.getFoodName() != null) {  
     foodButton.setText(component.getFoodName());  
   }  
   return convertView;  
 }  

But my Adapter is dynamic, I'd like to add more components. On adding component new "item view" appears on the bottom of ListView, and this "item view" reuse layout of previous. So component.getFoodName() == null,  and foodButton reuses caption from previous button.
Right thing is to set foodButton caption manually
foodButton.setText(R.string.select_food);

This issue don't seem difficult, but it drove me crazy for a while, till I figured it out.

No comments:

Post a Comment