开发者

Android - EditText as search box doesn't work

开发者 https://www.devze.com 2023-04-09 10:14 出处:网络
I would like to enable search functionality to EditText. My requirement is like this: when someone types with some letter, suppose the user types \'a\', then it should show all the words which starts

I would like to enable search functionality to EditText. My requirement is like this: when someone types with some letter, suppose the user types 'a', then it should show all the words which starts with 'a' should be shown as a drop down list. For example, if I first typed "copy" in the EditText, if again, I cleared the EditText and tried to type "co", then it should show the drop-down list "copy","come","cow".... I want to enable this feature to my EditText view. It worked when I had a ListView with only a textview inside, but now that I have two textview in my ListView it doesn't work.

This is my activity:

public class sedactivity extends Activity {


ListView lview;
ListViewAdapter lviewAdapter;
EditText ed;

private static final String first[] = {
    "America",
    "Busta",
    "Cactus",
    "Fire",
    "Garden",
    "Hollywood",
    "King",};

private static final String second[] = {
    "Uniti",
    "Chiusa",
    "Verde",
    "Fiamme",
    "Aperto",
    "Boulevard",
    "Kong",};

private ArrayList<String> 开发者_运维知识库arr_sort= new ArrayList<String>();
int textlength=0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ed = (EditText)findViewById(R.id.EditText1);
    lview = (ListView) findViewById(R.id.listView2);
    lviewAdapter = new ListViewAdapter(this, first, second);


    System.out.println("adapter => "+lviewAdapter.getCount());

    lview.setAdapter(lviewAdapter);

    lview.setTextFilterEnabled(true);



    ed.addTextChangedListener(new TextWatcher() {



    public void afterTextChanged(Editable s) {
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before, int count) {
            textlength=ed.getText().length();
            arr_sort.clear();
            for(int i=0;i<first.length;i++)
            {
            if(textlength<=first[i].length())
            {
            if(ed.getText().toString().equalsIgnoreCase((String) first[i].subSequence(0, textlength)))
            {
            arr_sort.add(first[i]);
            }
            }
            }

        }});}

}

And this is my ListView Adapter

public class ListViewAdapter extends BaseAdapter{
Activity context;
String title[];
String description[];

public ListViewAdapter(Activity context, String[] title, String[] description) {
    super();
    this.context = context;
    this.title = title;
    this.description = description;
}

public int getCount() {
    // TODO Auto-generated method stub
    return title.length;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

private class ViewHolder {
    TextView txtViewTitle;
    TextView txtViewDescription;
}

public View getView(int position, View convertView, ViewGroup parent)
{
    // TODO Auto-generated method stub
    ViewHolder holder;
    LayoutInflater inflater =  context.getLayoutInflater();

    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.listitem_row, null);
        holder = new ViewHolder();
        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
        holder.txtViewDescription = (TextView) convertView.findViewById(R.id.textView2);
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtViewTitle.setText(title[position]);
    holder.txtViewDescription.setText(description[position]);

return convertView;
}

}


I think an AutoCompleteTextView might be more suitable to implement this feature.


Check out this example to create your own Listener to check the values inside Edittext on key press event - How to implement your own Listener


Instead of extending BaseAdapter you extend ArrayAdapter. It implements the Filterable interface for you. In TextWatcher call getFilter().filter(text); method.

public void afterTextChanged(Editable s) {
    listview.getAdapter().getFilter().filter(s);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号