một vấn đề mà tôi không thể giải quyết được.Android AutoCompleteTextView với dữ liệu từ dịch vụ web, sự cố hiển thị danh sách đề xuất
Điều tôi muốn đạt được: Hiển thị đề xuất trong AutoCompleteTextView bắt đầu từ cuộc gọi dịch vụ web. Kết quả cuối cùng sẽ giống như thế này:
Những gì tôi đã làm như vậy cho đến nay:
Trong cách bố trí của tôi, tôi có một AutoCompleteTextView như thế này
<AutoCompleteTextView
android:id="@+id/txtAutoSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"
android:singleLine="true" />
On Hoạt động của tôi, tôi có này:
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());
}
}
});
Các AsyncTask là khá đơn giản, onPostExecute
tôi đặt bộ chuyển đổi với các dữ liệu trả về từ WebService
autoCompleteAdapter = new AdapterAutoComplete(context, ????? what should I have here?????,result);
txtAutoSearch.setAdapter(autoCompleteAdapter);
Các adapter trông như thế này:
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;
}
}
gì thực tế xảy ra:
- dịch vụ web trả về danh sách tôi cần, vì vậy dữ liệu có sẵn
- trong bộ chuyển đổi getView dường như không thực hiện vì vậy tôi đoán có cái gì đó sai trái được thực hiện trong đó.
- không có danh sách đề xuất nào được hiển thị với các giá trị của tôi
- Tôi không biết mình cần gì đối với bộ dựng bộ kết nối, cho textViewResourceId. Tôi đang làm gì sai? Tôi đang mắc kẹt xin vui lòng giúp đỡ.