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

时间:2011-12-09 12:40:23

标签: android listview autocomplete autocompletetextview

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

这是我的活动:

public class sedactivity extends Activity {


ListView lview;
ListViewAdapter lviewAdapter;
AutoCompleteTextView acTV;

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

acTV = (AutoCompleteTextView)findViewById(R.id.acTV);
lview = (ListView) findViewById(R.id.listView2);
lviewAdapter = new ListViewAdapter(this, first, second);


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

lview.setAdapter(lviewAdapter);

lview.setTextFilterEnabled(true);



acTV.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=acTV.getText().length();
        arr_sort.clear();
        for(int i=0;i<first.length;i++)
        {
        if(textlength<=first[i].length())
        {
        if(acTV.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;}}

0 个答案:

没有答案