我会使用来自 web-service-rest 的数据来使用 AutoCompleteTextView。困难在于将 ArrayAdapter 与 ViewHolder 一起使用。
这是我的 mainActivity.java 的一部分
final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
private boolean shouldAutoComplete = false;
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
shouldAutoComplete = true;
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
if(shouldAutoComplete){
try {
ArrayList<Intervento> interventos = getInterventos(autoCompleteTextView.getText().toString());
AutoCompleteAdapter adapter = new AutoCompleteAdapter(MainActivity.this,R.layout.suggest_list,interventos);
adapter.setNotifyOnChange(true);
autoCompleteTextView.setAdapter(adapter);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
这是我的自定义类 AutoCompleteAdapter
public class AutoCompleteAdapter extends ArrayAdapter<Intervento> implements Filterable {
static class ViewHolder{
private TextView textName;
private TextView textId;
}
private ArrayList<Intervento> arrayListIntervento;
private int layout;
@Override
public int getCount() {
return arrayListIntervento.size();
}
@Override
public Intervento getItem(int index) {
return arrayListIntervento.get(index);
}
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected void publishResults(CharSequence contraint, FilterResults results) {
if(results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
@Override
protected FilterResults performFiltering(CharSequence arg0) {
FilterResults filterResults = new FilterResults();
if(arg0 != null) {
try {
arrayListIntervento = new InterventoController().execute("http://192.168.1.162:8080/******/******/{*******}/****/**********", arg0.toString()).get();
}
catch(Exception e) {
e.printStackTrace();
}
// Now assign the values and count to the FilterResults object
filterResults.values = arrayListIntervento;
filterResults.count = arrayListIntervento.size();
}
return filterResults;
}
};
return myFilter;
}
@Override
public View getView(int position, View contentView, ViewGroup viewGroup) {
View view = null;
ViewHolder viewHolder = null;
if(contentView==null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.suggest_list, null);
if (view != null) {
viewHolder = new ViewHolder();
viewHolder.textId = (TextView) view.findViewById(R.id.idIntervento);
viewHolder.textName = (TextView) view.findViewById(R.id.nomeIntervento);
view.setTag(viewHolder);
}
}else {
view = contentView;
viewHolder = (ViewHolder) contentView.getTag();
}
if (viewHolder != null) {
Intervento intervento = arrayListIntervento.get(position);
if(intervento!=null){
viewHolder.textId.setText(intervento.getIdintervento().toString());
viewHolder.textId.setVisibility(View.INVISIBLE);
viewHolder.textName.setText(intervento.getNome());
}
}
return view;
}
public AutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.arrayListIntervento= new ArrayList<Intervento>();
this.layout=textViewResourceId;
}
}
这是我列出建议的自定义布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/nomeIntervento"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/autocompleteIntervento" />
<TextView
android:id="@+id/idIntervento"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/autocompleteIntervento" />
</RelativeLayout>
我可以获取数据,但我没有显示在屏幕上。哪里有问题?提前致谢!