Ad

Why Creating A New Variable And Using It When You Can Use The Old Variable?

- 1 answer

Why do we need to :-

  1. Create a View x.
  2. Then set x = a
  3. Then use a if command on x if you can directly use a.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        // check if the current view is reused else inflate the view
        View listItemView = convertView;
    
        if(listItemView == null){
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }
    

Instead , Why can't we do this?

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null){
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }
Ad

Answer

First and second examples are valid. Only if first case you are creating an local copy of your class variable. It is useless. Why are developers doing this way? Who knows :)

About inflating. Inflate operations some expensive, cause your adapter items are simular, it is possible to inflate view only once.

Ad
source: stackoverflow.com
Ad