這是一個我無法解決的問題。Android AutoCompleteTextView包含來自Web服務的數據,顯示建議列表的問題
我想要的內容: 顯示來自Web服務調用的AutoCompleteTextView中的建議。最後的結果應該是這樣的:
我所做至今:
在我的佈局,我有這樣的
<AutoCompleteTextView
android:id="@+id/txtAutoSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"
android:singleLine="true" />
在我的活動我有一個AutoCompleteTextView這個:
autoCompleteAdapter = new AdapterAutoComplete(context, ????? what should I have here?????, null);
autoCompleteAdapter.setNotifyOnChange(true);
txtAutoSearch.setAdapter(autoCompleteAdapter);
txtAutoSearch.addTextChangedListener(new TextWatcher() {
private boolean shouldAutoComplete = true;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
shouldAutoComplete = true;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (shouldAutoComplete) {
new GetAutoSuggestionsTask().execute(s.toString());
}
}
});
的的AsyncTask是非常簡單的,onPostExecute
我設置與數據適配器從WebService返回
autoCompleteAdapter = new AdapterAutoComplete(context, ????? what should I have here?????,result);
txtAutoSearch.setAdapter(autoCompleteAdapter);
適配器看起來是這樣的:
public class AdapterAutoComplete extends ArrayAdapter<Person> {
private Activity activity;
private List<Person> lstPersons;
private static LayoutInflater inflater = null;
public AdapterAutoComplete(Activity a, int textViewResourceId, List<Person> lst) {
super(a, textViewResourceId, lst);
activity = a;
lstPersons = lst;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return lstPersons.size();
}
public long getItemId(int position) {
return position;
}
public Person getCurrentPerson(int position) {
return lstPersons.get(position);
}
public void removeItem(int position) {
lstPersons.remove(position);
}
public static class ViewHolder {
public TextView txtName;
public TextView txtProfession;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.people_list_item, null);
Utils.overrideFonts(activity, vi);
holder = new ViewHolder();
holder.txtName = (TextView) vi.findViewById(R.id.txtName);
holder.txtProfession = (TextView) vi.findViewById(R.id.txtProfession);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
Person o = lstPersons.get(position);
if (o.name != null) {
holder.txtName.setText(o.name);
} else {
holder.txtName.setText("N/A");
}
if (o.profession != null) {
holder.txtProfession.setText(o.profession);
} else {
holder.txtProfession.setText("N/A");
}
return vi;
}
}
什麼實際發生的情況:
- Web服務返回我需要的列表,因此數據可用
- 在適配器getView似乎並沒有執行,所以我想有一些錯誤做了。
- 沒有與我的值顯示的建議列表
- 我不知道我需要爲Adapter構造函數,textViewResourceId。 我在做什麼錯?我卡住請幫助。