0

テキストの配列からアダプタにコンテンツを設定するためにアレイアダプタを作成しようとしていますが、アクティビティからアダプタを設定できません。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

拡張してカスタム アダプターを作成している場合は、デフォルトの Android レイアウトの代わりに行レイアウト ID を渡しますArrayAdapter

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

それ以外の

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