Searchable list view through Edit text

2019-06-13 08:19发布

问题:

hello everyone I want develop application which have many item in array list and one Edit text for using search item fast in list view I show u image like that:

so how it possible in page

回答1:

You need to add addTextChangedListener to your EditText. when user enters a new data in EditText , get the text from it and passing it to array adapter filter. you can add like this :

inputSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text
    MainActivity.this.adapter.getFilter().filter(cs);  
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub                         
}

});

Full example is here



回答2:

Try Auto Complete Text View available in Android.Its Edit text with Search functionality in android.

You dont need to write code for filtering with the use of Auto Complete Text view

For source code have a look at this sample.

You can ask if you have any queries.Happy coding :)



回答3:

have you checked Arrayadapter's filter method you need to set textwatcher and in textchange method you need to call adapter.filter("word entered in edittext") and you done for more detail check this



回答4:

What you're trying to do might be achieved with AutoCompleteTextView, so you'll have to change your EditText by it.

Further, you'll need to extend your own ArrayAdapter, and inside it, extend your Filter class and declare an instance of your extended filter class. In the Filter extension, you'll need to override the following methods:

  • FilterResults performFiltering(CharSequence): There you implement how you want to filter.
  • void publishResults(CharSequence, final FilterResults): You'll receive here the results you've returned from FilterResults as the second parameter. Simply call notifyDataSetChanged() if you have more than one item.

This way you'll be able to filter the way you want. A good example might be found here.



回答5:

Try this Simple Code: XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SOF_AutoList" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Name" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Java Activity Class:

public class SOF_AutoList extends Activity
{
    private ListView listview;
    private EditText edttxt;
    private ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sof__auto_list);

        ArrayList<String> data = new ArrayList<String>();
        for(int i=0;i<10;i++)
        {
            if(i==0 || i==1 || i==3)
            {
                data.add("Apple "+(i+1));
            }
            else if(i==4 || i==5)
            {
                data.add("Banana "+(i+1));
            }
            else
            {
                data.add("CArrot "+(i+1));
            }
        }

        listview = (ListView)findViewById(R.id.listView1);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,data);
        listview.setAdapter(adapter);

        edttxt = (EditText)findViewById(R.id.editText1);
        edttxt.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                adapter.getFilter().filter(s);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
    }


}


回答6:

I used this code to make an auto complete Edittext by using a simple edittext.

public class MainActivity extends Activity implements OnKeyListener {

EditText e;
String[] st={"apple","abhay","anurag","boll","banana","basic","cat","calm"};
String[] temp={"","","","","",""};
ArrayAdapter<String> ad;
ListView lv;
ViewGroup vg;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    vg=(ViewGroup)findViewById(R.id.LinearLayout1);
    e=(EditText)findViewById(R.id.editText1);
    e.setOnKeyListener(this);
    ad=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1, temp);
    lv=new ListView(getBaseContext());
    lv.setAdapter(ad);
    lv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
    // TODO Auto-generated method stub
    Log.d("", "onKey Entered");
    if(KeyEvent.ACTION_UP==arg2.getAction())
    {

    vg.removeView(lv);
    String s=e.getText().toString();
    Log.d("", "onKey if Entered"+s);
    for(int i=0,j=0;i<st.length;i++,j++)
    {
        if(st[i].startsWith(s))
            temp[j]=st[i];
        else
            --j;

    }
    vg.addView(lv);

}
    return false;

}



回答7:

my code is here

ArrayList<Integer> ids=new ArrayList<Integer>();
    ArrayList<String>  ar=new ArrayList<String>();

    ArrayAdapter  adpter;

    private ListView list;
    private EditText search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        search=(EditText)findViewById(R.id.tvsearch);

        ActionBar ab=getActionBar();

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

        handler h=new handler(MainActivity.this);
        ArrayList<property> p = h.display();
        StringBuffer sb;

        for(property p1 : p)
        {
            sb=new StringBuffer();
            int id=p1.getId();
            String State_Name = p1.getState_name();
            String State_Web= p1.getState_web();

            ar.add(State_Name);
            ids.add(id);

        }
            h.close();


            adpter=new ArrayAdapter(MainActivity.this, android.R.layout.simple_selectable_list_item,ar);
            list.setAdapter(adpter);

            search.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    MainActivity.this.adpter.getFilter().filter(s); 

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub

                }
            });

    //  list.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_selectable_list_item,ar));
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos,
                    long id) 
            {
                int did=ids.get(pos);
                Intent i=new Intent(MainActivity.this,website.class);
                i.putExtra("did",did);
                startActivity(i);
            }
        });

    }