Android - EditText作为搜索框不起作用

时间:2011-09-30 18:48:07

标签: android android-edittext

我想为EditText启用搜索功能。我的要求是这样的: 当某人输入某个字母时,假设用户键入“a”,那么它应该显示所有以“a”开头的单词应显示为下拉列表。例如,如果我首先在EditText中键入“copy”,如果再次,我清除了EditText并尝试键入“co”,那么它应该显示下拉列表“copy”,“come”,“cow”。 ... 我想在EditText视图中启用此功能。当我有一个只有一个textview的ListView时,它工作,但现在我的ListView中有两个textview,它不起作用。

这是我的活动:

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

        }});}

}

这是我的ListView适配器

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

}

3 个答案:

答案 0 :(得分:5)

我认为AutoCompleteTextView可能更适合实现此功能。

答案 1 :(得分:2)

查看此示例以创建您自己的侦听器,以便在按键事件How to implement your own Listener

上检查Edittext内的值

答案 2 :(得分:0)

而不是扩展BaseAdapter,而是扩展ArrayAdapter。它为您实现Filterable接口。在TextWatcher调用getFilter().filter(text);方法。

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