这是我为类似问题所做的事情。
我有使用适配器来设置自动完成文本视图。
这是我的PlacesAutoCompleteAdapter.java文件
package com.inukshk.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import com.inukshk.CreateInukshk.CreateInukshk;
public class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements
Filterable {
private ArrayList<String> resultList;
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = CreateInukshk.autocomplete(constraint
.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
这里我使用了 resultList = CreateInukshk.autocomplete(constraint .toString()); 这是我的方法,它将返回我想要显示的数组列表。
最后这是我在主java文件中的代码,我已经初始化了我们的AutoCompleteTextview。
内部 OnCreate();
autoCompView = (AutoCompleteTextView) findViewById(R.id.editloc);
autoCompView.setAdapter(new PlacesAutoCompleteAdapter(this,
R.layout.list_item));
// autoCompView.setOnItemClickListener(CreateInukshk.this);
autoCompView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// here you can get value of arg2 as your position while selecting value.
// Place = autoCompView.getText().toString();
new AsyncGetAutoPlace().execute(autoCompView.getText()
.toString().trim());
}
});
而不是 new AsyncGetAutoPlace().execute(autoCompView.getText() .toString().trim()); 您可以添加任何您想做的代码。
希望它会帮助你。