得到Custom ArrayAdapter的Count()没有被调用

时间:2014-08-27 16:24:50

标签: android adapter android-arrayadapter

我编写了一个Custom ArrayAdapter的代码,我试图与AutoCompleteTextView集成。 Adapter的代码如下所示:

package com.iincore.msgis.adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import android.widget.TextView;

import com.esri.core.tasks.geocode.LocatorGeocodeResult;
import com.iincore.msgis.offlinenavigation.R;

public class AutoCompleteCustomArrayAdapter extends ArrayAdapter<LocatorGeocodeResult>{

Context         context;
int         layoutResourceId;
List<LocatorGeocodeResult> data;


public AutoCompleteCustomArrayAdapter(Context context, int resource,      List<LocatorGeocodeResult> objects) {
super(context, resource, objects);
this.context = context;
this.layoutResourceId = resource;
this.data = objects;

}

@Override
public int getCount() {
    return data.size();
}

@Override
public LocatorGeocodeResult getItem(int position) {
    return data.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
    if (convertView == null) {
    // inflate the layout
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(layoutResourceId, parent, false);
    }

    // LocatorGeocodeResult item based on the position
    LocatorGeocodeResult objectItem = data.get(position);

    // get the TextView and then set the text (item name) and tag (item ID) values
    TextView textViewItem = (TextView) convertView.findViewById(R.id.suggestion_search);
    textViewItem.setText(objectItem.getAddress());
    } catch (NullPointerException e) {
    e.printStackTrace();
   } catch (Exception e) {
    e.printStackTrace();
   }
    return convertView;
   }



}

我将此适配器设置为AsyncTask的AutoCompleteTextView方法中的onPostExecute(),我将发送结果(这是一个List)作为AutoCompleteCustomArrayAdapter构造函数的参数之一。 onPostExecute()方法如下所示:

  @Override
protected void onPostExecute(List<LocatorGeocodeResult> result) {
super.onPostExecute(result);
AutoCompleteCustomArrayAdapter adapter;
try {
  adapter= new AutoCompleteCustomArrayAdapter(context, R.layout.list_item, result);
    searchBox.setAdapter(adapter);
} catch (Exception e) {
    e.printStackTrace();
}

}

此处context是MainActivity的上下文,它设置主要布局,searchBox是我从MainActivity传递的AutoCompleteTextView对象。

如果我调试我的代码,我可以看到构造函数被调用,但getCount()方法和getView()方法没有被调用。

0 个答案:

没有答案