0

我正在尝试创建一个数组适配器以将文本数组中的内容设置到适配器,但我无法从 Activity 设置适配器。我不确定我应该将什么作为 Int 传递给资源

PlacesListAdapter.java

public class PlacesListAdapter extends ArrayAdapter<Place> {
    public Context context;
    private List<Place> places;

    public PlacesListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public PlacesListAdapter(Context context, int resource, List<Place> places) {
        super(context, resource, places);
        this.context = context;
        this.places = places;
        // imageLoader = new ImageLoader(context.getApplicationContext());

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        View view = convertView;
        Place p = places.get(position);

        if (convertView == null) {
            LayoutInflater viewInflater;
            viewInflater = LayoutInflater.from(getContext());
            view = viewInflater.inflate(R.layout.item_place, null);

            holder = new ViewHolder();
            holder.placeTitle = (TextView) view.findViewById(R.id.place_title);
            holder.placeDistance = (TextView) view
                    .findViewById(R.id.place_distance);

            view.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.placeTitle.setText(p.getPlaceTitle());
        holder.placeDistance.setText("200");
        holder.placeCategoryIcon.setImageResource(R.drawable.marker);

        return view;
    }

    static class ViewHolder {
        TextView placeId;
        TextView placeTitle;
        TextView placeDistance;
        ImageView placeCategoryIcon;
    }

}

Place.java

public class Place {

    String placeId = "", placeTitle = "", placeDistance = "",
            placeCategoryIcon = "";

    public Place(String placeId, String placeTitle, String placeDistance,
            String placeCategoryIcon) {
        this.placeId = placeId;
        this.placeTitle = placeTitle;
        this.placeDistance = placeDistance;
        this.placeCategoryIcon = placeCategoryIcon;
    }

    public String getPlaceId() {
        return placeId;
    }

    public void setPlaceId(String placeId) {
        this.placeId = placeId;
    }

    public String getPlaceTitle() {
        return placeTitle;
    }

    public void setPlaceTitle(String placeTitle) {
        this.placeTitle = placeTitle;
    }

    public String getPlaceDistance() {
        return placeDistance;
    }

    public void setPlaceDistance(String placeDistance) {
        this.placeDistance = placeDistance;
    }

    public String getPlaceCategoryIcon() {
        return placeCategoryIcon;
    }

    public void setPlaceCategoryIcon(String placeCategoryIcon) {
        this.placeCategoryIcon = placeCategoryIcon;
    }

}

现在在 MainActiivty 我正在尝试设置适配器,以便它填充数组中的列表

public class MainActivity extends SherlockFragmentActivity implements
        SearchView.OnQueryTextListener {

    private final String[] places = new String[] { "Mysore", "Bangalore", "Mangalore",
            "Wayanad", "Bandipur National Park", "Chickmaglur",
            "Bandipura", "Coorg", "Kodaikanal", "Hampi",
            "Ghati Subramanya", "Mekedatu", "Muththathhi", "Shivasamudram",
            "Talakadu", "Savana Durga" };

    public SearchView mSearchView;
    private TextView mStatusView;

    private Menu mainMenu = null;

    PlacesListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i("Nomad", "onCreate");

        ListView listView = (ListView) findViewById(R.id.place_list);


        adapter = new PlacesListAdapter(this, resource, places);

    }
}

我不确定要设置resources什么adapter = new PlacesListAdapter(this, resource, places);

4

2 回答 2

1

初始化resource变量:

// it doesn't matter what values you assing to the resource variable because you build
// the row layout yourself in the getView method of the adapter
int resource = android.R.layout.simple_list_item_1;
adapter = new PlacesListAdapter(this, resource, places);

编辑:

List<Place> thePlaces = new ArrayList<Place>();
for (int i = 0; i < places.length; i++) {
     Place pl = new Place("NO_ID", places[i], "NO_DISTANCE", "NO_CATEGORYICON");
     thePlaces.add(pl);
}
int resource = android.R.layout.simple_list_item_1;
adapter = new PlacesListAdapter(this, resource, thePlaces);
于 2012-12-10T10:04:14.337 回答
1

如果您通过扩展创建自定义适配器,则传递行布局 ID 而不是默认的 android 布局ArrayAdapter

adapter = new PlacesListAdapter(this,R.layout.item_place, places);

代替

adapter = new PlacesListAdapter(this, resource, places);
于 2012-12-10T10:15:50.047 回答