0

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:

enter image description here

so how it possible in page

Ravi
  • 34,851
  • 21
  • 122
  • 183
  • 1
    Did you try to implement it . Please Post your code?? – Md Abdul Gafur Feb 03 '14 at 10:49
  • I have one list view and one Edit text list view get values from array list and there are many item in array and I want make like I am type Gu in Edit text and i got all values starer Gu like Gujarat –  Feb 03 '14 at 10:53
  • Just check this out Whole example [here][1]. [1]: http://stackoverflow.com/questions/21501356/search-friends-like-facebook/21505295#21505295 – Sagar Shah Feb 03 '14 at 10:57
  • http://developer.android.com/guide/topics/ui/controls/text.html .. Just goto to this link may it provide u with some solution. – Ashoka Feb 03 '14 at 10:58

7 Answers7

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 :)

Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27
0

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

Maulik.J
  • 636
  • 1
  • 6
  • 14
  • i got some problem with these when i using adapter and addtextxhanger but when i got result of this filter i got values of array not database id –  Feb 03 '14 at 11:30
  • can you please let me know what exactly you want to do? do you want id on listview item click or at textchange? – Maulik.J Feb 03 '14 at 11:34
  • i have one list view and one Edit text and list view got values from Database and set in List view and database has three column id,name,site name so i want to go those website using list view item with help of searchable Edit text thats it –  Feb 03 '14 at 11:38
  • then when you select any item then just query(select query) that value to get site equivalent to that value and just open the same. – Maulik.J Feb 03 '14 at 11:41
  • i put my code check it i think its issue with array list –  Feb 03 '14 at 11:45
0

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.

Community
  • 1
  • 1
nKn
  • 13,691
  • 9
  • 45
  • 62
0

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

Brinda K
  • 745
  • 3
  • 16
  • 34
0

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

            }
        });
    }


}
Sagar Shah
  • 4,272
  • 2
  • 25
  • 36
0

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;

}

Akshay Paliwal
  • 3,718
  • 2
  • 39
  • 43
0

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);
            }
        });

    }
  • in this case you will get wrong id you need to query like this int did = ids.get(ar.indexOf(selected item(state name))) and you will get proper answer – Maulik.J Feb 03 '14 at 11:47