Question

I should do so that, by having a list of names in a gridview, from a search bar I can:

  1. Make a real-time-search (like google search for instance)
  2. to remove the names that do not begin with the letters you type (for example if you type 'ja' or 'Ja' or 'JA' the app deletes all the names in the list except 'Jack)

I don't know where to start.. Can you help me?

Here's People.java:

package com.ec.people;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;


public class people extends Activity {
    GridView peoplelist;
    EditText search;

    static final String[] numbers = new String[] { 
        "Rossi", "Loggia", "Boni",
        "Milanesi", "Mancini", "Cremonesi",
        "Dali - Colombo", "Fiorentini",
        "Trevisani", "Monaldo", "Udinesi - Pagnotto",
        "Beneventi", "Zucchi", "Calabri",};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_people);               

        search = (EditText)findViewById(R.id.editText1);            
        peoplelist = (GridView)findViewById(R.id.gridView1);            

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, numbers);
        peoplelist.setAdapter(adapter);

        peoplelist.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(getApplicationContext(),
                    ((TextView) v) .getText(), Toast.LENGTH_SHORT) .show();
            }


        });

        // I use this to perform search

                    search.addTextChangedListener(new TextWatcher() {

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {




                            for(int i=0; i < numbers.length ; i++)
                            {
                            if(numbers[1].startsWith(s.toString())  )
                            {


                            }
                            }

                            //PerformSearch mysearch  = new PerformSearch();



                        }


                        @Override
                        public void afterTextChanged(Editable s) { }

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

    }
}       
Was it helpful?

Solution

You can implement it in two ways:

1 ) AutoCompleteTextView

2 ) Using getFilter()

I am including both example as well as screenshots:

1) AutoCompleteTextView

your xml file activity_main.xml

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
xmlns:tools="http://schemas.android.com/tools"  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
tools:context=".MainActivity" >  

<TextView  
    android:id="@+id/textView1"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_alignParentLeft="true"  
    android:layout_alignParentTop="true"  
    android:layout_marginTop="15dp"  
    android:text="@string/what_is_your_favourite_programming_language_" />  

<AutoCompleteTextView  
    android:id="@+id/autoCompleteTextView1"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_alignParentLeft="true"  
    android:layout_below="@+id/textView1"  
    android:layout_marginLeft="36dp"  
    android:layout_marginTop="17dp"  
    android:ems="10"  
    android:text="">  

    <requestFocus />  
</AutoCompleteTextView>  

your java file MainActivity.java

    public class MainActivity extends Activity {  
    String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        //Creating the instance of ArrayAdapter containing list of language names  
           ArrayAdapter<String> adapter = new ArrayAdapter<String>  
            (this,android.R.layout.select_dialog_item,language);  
        //Getting the instance of AutoCompleteTextView  
           AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);  
           actv.setThreshold(1);//will start working from first character  
           actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView  
           actv.setTextColor(Color.RED);  

    }  

    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_main, menu);  
        return true;  
    }  

}  

screenshot :

enter image description here

2) Using getFilter()

I have already answered this. look at here

screenshot:

enter image description here enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top