Android执行实时搜索

时间:2014-04-18 09:03:38

标签: android eclipse list search real-time

我应该这样做,通过在gridview中有一个名单列表,我可以从搜索栏中找到:

  1. 进行实时搜索(例如谷歌搜索)
  2. 删除不以您键入的字母开头的名称(for 例如,如果您输入' ja'或者' Ja'或者' JA'该应用删除所有 列表中的名称除了'杰克)
  3. 我不知道从哪里开始.. 你能救我吗?

    这里是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) {}
                        });
    
        }
    }       
    

1 个答案:

答案 0 :(得分:2)

您可以通过两种方式实现它:

1)AutoCompleteTextView

2)使用getFilter()

我包括示例和截图:

1)AutoCompleteTextView

您的xml文件 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>  

您的java文件 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;  
    }  

}  

屏幕截图:

enter image description here

2)使用getFilter()

我已经回答了这个问题。看看here

截图:

enter image description here enter image description here