1

What is the all about if(converView==null) { } else { } What if i avoid writing else part and how it effects my code ? I just wanted to know if it works fine without else part . Could someone explain about the gettag and settag for convertview???

            public View getView(int position, View convertView, ViewGroup parent) {
        DeviceViewHolder holder = null;
        mSelectedDeviceBin = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.single_device_view, null);
            holder = new DeviceViewHolder();
            holder.deviceId = (TextView) convertView
                    .findViewById(R.id.deviceId);

            holder.deviceType = (TextView) convertView
                    .findViewById(R.id.deviceType);

            convertView.setTag(holder);

        } else

            holder = (DeviceViewHolder) convertView.getTag();

        // Populating the views
        holder.deviceId.setText(mSelectedDeviceBin.getDeviceBinId());

        StringBuilder deviceCount = new StringBuilder();
        deviceCount.append("");
        double count = mSelectedDeviceBin.getQtyStock();
        deviceCount.append(count);
        String deviceCountString = deviceCount.toString();
        holder.deviceType.setText("Total number of Items:"
                + deviceCountString);

        return convertView;
    }
user2680801
  • 89
  • 2
  • 3
  • 7
  • 2
    [The world of `ListView`](http://www.youtube.com/watch?v=wDBM6wVEO70) is the best answer. It's an 1 hour Google IO presentation, but it's not a wasted time – gunar Oct 23 '13 at 13:59
  • 2
    possible duplicate of [How ListView's recycling mechanism works](http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works) – Steve Benett Oct 23 '13 at 14:07

1 Answers1

6

The implementation uses convertViews and the ViewHolder pattern, and works as follows.

Instead of inflating a new view for each item in your list, Android only creates so many views as are visible. Once a view goes out of screen, it will be reused. This way, the number of inflate calls, which are relatively slow, are minimized.

Secondly, the ViewHolder pattern makes sure the number of view finds (findViewById() ) are minimized, because this is relatively slow as well, especially when scrolling. So instead of finding the views every time, direct references are kept in memory, and stored in the ViewHolder. To make sure the correct ViewHolder object is stored with the correct item view, they are set inside the convertView with view.setTag(). They can be obtained by using the getTag() call.

The else is required, because otherwise the viewHolder variable would be null, and the app would crash.

For clarity I would advice you to write the else like this:

} else {
    holder = (DeviceViewHolder) convertView.getTag();
}

Edit: also, you can move the creation of the LayoutInflator inside the if(convertView == null){} statement. It is not needed otherwise.

Jeffrey Klardie
  • 3,020
  • 1
  • 18
  • 23
  • In my case using the above if-else results in one holder attaching to two ConvertViews, which ultimately leads to a click on one row and events on two rows being fired simultaneously............. – Karan Apr 09 '15 at 06:02
  • 1
    Please make a new question, considering your two line comment can in no way help anyone here, nor can we help you. – Jeffrey Klardie Apr 09 '15 at 07:39