Android Lesson 7 – part 3: work with ListView


We can cache view lookup for improve performance, here’s the solution:

View lookup cache:

private class ViewCache {
        TextView serverID;
        TextView name;
    }

The custom ArrayAdapter

    public class CityAdapter extends ArrayAdapter<City> {
        public CityAdapter(Context context, ArrayList<City> cities) {
            super(context, 0, cities);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the data item for this position
            City city = getItem(position);
            ViewCache viewCache;
            if (convertView == null) {
                viewCache = new ViewCache();
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(R.layout.item, parent, false);
                viewCache.serverID = (TextView) convertView.findViewById(R.id.serverID);
                viewCache.name = (TextView) convertView.findViewById(R.id.name);
                convertView.setTag(viewCache);
            } else {
                viewCache = (ViewCache) convertView.getTag();
            }
            // Populate the data into the template view using the data object
            viewCache.serverID.setText(city.serverID);
            viewCache.name.setText(city.name);
            // Return the completed view to render on screen
            return convertView;
        }
    }

Here’s the full code of file MainActivity.java

package com.tutorialspots.listview;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    public ListView listView;

    public class City {
        public String serverID;
        public String name;
        public City(String serverID, String name) {
            this.serverID = serverID;
            this.name = name;
        }
    }

    private class ViewCache {
        TextView serverID;
        TextView name;
    }

    public class CityAdapter extends ArrayAdapter<City> {
        public CityAdapter(Context context, ArrayList<City> cities) {
            super(context, 0, cities);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the data item for this position
            City city = getItem(position);
            ViewCache viewCache;
            if (convertView == null) {
                viewCache = new ViewCache();
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(R.layout.item, parent, false);
                viewCache.serverID = (TextView) convertView.findViewById(R.id.serverID);
                viewCache.name = (TextView) convertView.findViewById(R.id.name);
                convertView.setTag(viewCache);
            } else {
                viewCache = (ViewCache) convertView.getTag();
            }
            // Populate the data into the template view using the data object
            viewCache.serverID.setText(city.serverID);
            viewCache.name.setText(city.name);
            // Return the completed view to render on screen
            return convertView;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        listView = (ListView) findViewById(R.id.listView);

        // Construct the data source
        ArrayList<City> arrayOfCities = new ArrayList<City>();

        arrayOfCities.add(new City("sv1", "London"));
        arrayOfCities.add(new City("sv2", "Manchester"));
        arrayOfCities.add(new City("sv3", "Newyork"));
        arrayOfCities.add(new City("sv4", "Toronto"));
        arrayOfCities.add(new City("sv5", "Athen"));
        arrayOfCities.add(new City("sv6", "Hanoi"));
        arrayOfCities.add(new City("sv7", "Bangkok"));
        arrayOfCities.add(new City("sv8", "Jakarta"));
        arrayOfCities.add(new City("sv9", "Sydney"));
        arrayOfCities.add(new City("sv10", "Tokyo"));
        arrayOfCities.add(new City("sv11", "Jerusalem"));
        arrayOfCities.add(new City("sv12", "Istanbul"));
        arrayOfCities.add(new City("sv13", "Cairo"));
        arrayOfCities.add(new City("sv14", "Mexico"));
        arrayOfCities.add(new City("sv16", "Singapore"));
        arrayOfCities.add(new City("sv17", "Kualalampua"));
        arrayOfCities.add(new City("sv18", "Kabul"));
        arrayOfCities.add(new City("sv19", "Moscow"));
        arrayOfCities.add(new City("sv20", "Kiev"));
        arrayOfCities.add(new City("sv21", "Paris"));
        arrayOfCities.add(new City("sv22", "Berlin"));
        arrayOfCities.add(new City("sv23", "Vientiane"));
        arrayOfCities.add(new City("sv24", "La Habana"));
        arrayOfCities.add(new City("sv25", "Caracas"));

        // Create the adapter to convert the array to views
        CityAdapter adapter = new CityAdapter(this, arrayOfCities);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                City cityselected = (City) listView.getItemAtPosition(i);
                //String serverID = cityselected.serverID;
                String name = cityselected.name;
                Toast.makeText(getApplicationContext(), "City selected: " + name, Toast.LENGTH_LONG).show();
            }
        });
    }
...
}

The result we get:

android viewcache

Leave a Reply