Web-service-rest からのデータを使用して AutoCompleteTextView を使用します。問題は、ViewHolder で ArrayAdapter を使用することにあります。
これは私の 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>
データを取得できますが、画面に表示されません。問題はどこだ?前もって感謝します!