Posts Tagged ListView

How to create filter for ListView items in Android. Part-2. List with custom adapter.

As you can see in my previous post about ListView filtering in Android (How to create filter for ListView items in Android.) creating filter is pretty simple if you’re have list with Strings.

Here I show you how to create filter for your custom adapter with complex objects. Here the screenshot of sample application:

How to create filter for ListView items in Android. Part-2. List with custom adapter.

How to create filter for ListView items in Android. Part-2. List with custom adapter.

Here is updated version of this (How to create filter for ListView items in Android.) application.

In the list of countries you also can see population and area of the country.

Please look at Country class:

package com.hrupin.sample.filterlist;

public class Country {

    private String name;
    private int area;
    private int population;

    public Country(String name, int population, int area) {
        this.name = name;
        this.area = area;
        this.population = population;
    }
    
    public int getArea() {
        return area;
    }
    
    public String getName() {
        return name;
    }
    
    public int getPopulation() {
        return population;
    }
    
    @Override
    public String toString() {
        return this.name;
    }

}

Note: Take a look at toString() method. This is main thing. This is method used while filtering.

Adapter for Countries list. Here is nothing new:

package com.hrupin.sample.filterlist;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CountryListAdapter extends ArrayAdapter<Country> {
    private LayoutInflater vi;
    private static int mResource = R.layout.item_list;

    public CountryListAdapter(Activity activity, final List<Country> items) {
        super(activity, mResource, items);
        vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public static class ViewHolder {
        private TextView textViewItemCountryName;
        private TextView textViewItemCountryArea;
        private TextView textViewItemCountryPopulation;

        public ViewHolder(View v) {
            this.textViewItemCountryName = (TextView) v.findViewById(R.id.textViewItemCountryName);
            this.textViewItemCountryArea = (TextView) v.findViewById(R.id.textViewItemCountryArea);
            this.textViewItemCountryPopulation = (TextView) v.findViewById(R.id.textViewItemCountryPopulation);
        }
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = vi.inflate(mResource, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Country item = getItem(position);
        if (item != null) {
            holder.textViewItemCountryName.setText(Html.fromHtml(item.getName()));
            holder.textViewItemCountryArea.setText(Html.fromHtml("area = " + item.getArea() + " sq.km"));
            holder.textViewItemCountryPopulation.setText(Html.fromHtml("population = " + item.getPopulation()));

        }
        return convertView;
    }
}

And Activity. Take a look at TextWatcher:

package com.hrupin.sample.filterlist;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class FilterListSampleActivity extends Activity implements TextWatcher {
    
    private static List<Country> countries = Storage.getItems();
    private EditText editTextFilter;
    private ListView listViewCountries;
    private CountryListAdapter adapter;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        adapter = new CountryListAdapter(this, countries);
        
        editTextFilter = (EditText)findViewById(R.id.editTextFilter);
        editTextFilter.addTextChangedListener(this);
        
        listViewCountries = (ListView)findViewById(R.id.listViewCountries);
        listViewCountries.setAdapter(adapter);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s);
    }

    @Override
    public void afterTextChanged(Editable s) {}
    
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

}

That’s all.
Download the source code. Here you can download the source code of sample Android project

Tags: , , , ,

How to create filter for ListView items in Android.

Please. have a look at screenshot.

Filter for ListView items in Android

Filter for ListView items in Android

Here I want to show you how to create filter for ListView items. In this post my ListView contains simple strings.
As you see there the EditText on the screen. User can type text in this EditText and ListView will filter using user’s text in EditText.

This code works very fast.

Please have a look source code:

package com.hrupin.sample.filterlist;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class FilterListSampleActivity extends Activity implements TextWatcher {
    
    private static List<String> countries = Storage.getCountries();
    private EditText editTextFilter;
    private ListView listViewCountries;
    private ArrayAdapter<String> adapter;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
        
        editTextFilter = (EditText)findViewById(R.id.editTextFilter);
        editTextFilter.addTextChangedListener(this);
        
        listViewCountries = (ListView)findViewById(R.id.listViewCountries);
        listViewCountries.setAdapter(adapter);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s);
    }

    @Override
    public void afterTextChanged(Editable s) {}
    
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

}

All functionality goes around TextChangedListener which added in EditText.

In the next post (How to create filter for ListView items in Android. Part-2. List with custom adapter.) I’ll show you how to create filter for ListView with custom adapter and complex objects in it.

That’s all.
Download the source code. Here you can download the source code of sample Android project

Tags: , , , , ,

Hire me!