将两个不同的对象填充到单个ListView中

时间:2014-03-31 13:49:12

标签: java android android-listview

有两个对象:

public class Restaurant {
    public String name;
    public String address;
    public int type;
    public double duration;
    public double lat;
    public double lng;
    public boolean isStar;
    public int contact;
}

public class Spot {
    public String name;
    public String address;
    public int type;
    public double duration;
    public double lat;
    public double lng;
    public boolean isStar;
}

我有两个数组List的每个对象,我想把它填入ListView,注意它们之间的唯一区别是餐厅有联系字段。适配器就像这样

public class ResultAdapter extends ArrayAdapter<Restaurant> {

public ResultAdapter(Context context, int resource, List<Restaurant> items) {
    super(context, resource, items);
}

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

    View v = convertView;

    if (v == null) {
        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v =  vi.inflate(R.layout.result_row, null);
    }

    Restaurant p = (Restaurant) getItem(position);

    if (p != null) {
        TextView duration = (TextView) v.findViewById(R.id.duration);
        TextView name = (TextView) v.findViewById(R.id.name);
        TextView address = (TextView) v.findViewById(R.id.address);
        TextView type = (TextView) v.findViewById(R.id.type);
        TextView contact = (TextView) v.findViewById(R.id.contact);

        nameView.setText(p.name);
        addressView.setText(p.address);
        typeView.setText(p.type == 1 ? "Chinese Cuisine" : "Western Cuisine");
        contactView.setText(""+p.contact);
    }

    return v;

}

}

问题是如何在这种情况下创建适配器,因为适配器假设提供一种数据类型?我是否需要重新设计结构/只需创建一个新的Result对象?

更新

public class Restaurant extends Spot{
    public String name;
    public String address;
    public int type;
    public double duration;
    public double lat;
    public double lng;
    public boolean isStar;
    public int contact;

    public Restaurant(String _name, String _address, int _type, double _duration, double _lat, double _lng, boolean _isStar, int _contact) {
        name = _name;
        address = _address;
        type = _type;
        duration = _duration;
        lat = _lat;
        lng = _lng;
        isStar = _isStar;
        contact = _contact;
    }

}

抱歉在OO中很弱,如何更改构造函数?

3 个答案:

答案 0 :(得分:1)

这不是一个安卓问题。

只需让您的Restaurant成为Spot的子类型,并拥有单个List<Spot>和单个适配器来管理该列表。您也可以将Restaurant个对象保留在那里,因为它们是Spot s:

public class Spot {
    public String name;
    public String address;
    public int type;
    public double duration;
    public double lat;
    public double lng;
    public boolean isStar;
}

public class Restaurant extends Spot {
    /* the rest is inherited */
    public int contact;
}

您的适配器将

//it can hold Both Spot & Restaurant now as well.
public class ResultAdapter extends ArrayAdapter<Spot> { 

getView()

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

    // .. inflate View

    Spot s = (Spot) getItem(position);

    // .. use fields from Spot that are common to fill list view item

    // add info specific to restaurants
    if (s instanceof Restaurant) {
        // fill in extra contact info:
        contactView.setText(((Restauarant) s).contact);
    }

    return v;

}

答案 1 :(得分:1)

你需要创建一对对的集合;每对将包含两个对象 - 餐厅和现场。

List<Pair<Restaurant, Spot>> objects = new ArrayList<Pair<Restaurant, Spot>>();

在getView方法中,您将能够在特殊位置获得一对

Pair<Restaurant, Spot> pair = objects.get(position);
Restaurant myRestaurant = pair.first;
Spot mySpot = pair.second;

这不是唯一的解决方案,但它会起作用。

答案 2 :(得分:1)

你可以做两件事。首先,由于您的Restaurant对象几乎与您的Spot对象相同,因此可以像其他对象一样延伸:

public class Spot {
    public String name;
    public String address;
    public int type;
    public double duration;
    public double lat;
    public double lng;
    public boolean isStar;

    public Spot(String _name, String _address, int _type, double _duration, double _lat, double _lng, boolean _isStar) {
        name = _name;
        address = _address;
        type = _type;
        duration = _duration;
        lat = _lat;
        lng = _lng;
        isStar = _isStar;
    }
}

public class Restaurant extends Spot {
    public int contact;

    public Restaurant(String _name, String _address, int _type, double _duration, double _lat, double _lng, boolean _isStar, int _contact) {
        super(_name, _address, _type, _duration, _lat, _lng, _isStar);
        contact = _contact;
    }
}

然后,对于适配器,您可以创建一个使用Object的适配器,因为所有创建的对象都是从Object类隐式扩展的。之后,您只需检查每个对象的类型,如下所示:

public class ResultAdapter extends ArrayAdapter<Object> {

public ResultAdapter(Context context, int resource, List<Object> items) {
    super(context, resource, items);
}

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

    View v = convertView;

    if (v == null) {
        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v =  vi.inflate(R.layout.result_row, null);
    }


    TextView duration = (TextView) v.findViewById(R.id.duration);
    TextView name = (TextView) v.findViewById(R.id.name);
    TextView address = (TextView) v.findViewById(R.id.address);
    TextView type = (TextView) v.findViewById(R.id.type);
    TextView contact = (TextView) v.findViewById(R.id.contact);

    Spot spot = null;
    Restaurant restaurant = null;
    Object object = getItem(postion);
    if (object.getClass().isAssignableFrom(Restaurant.class) {
        restaurant = object;
    }
    if (object.getClass().isAssignableFrom(Spot.class) {
        spot = object;
    }

    if (spot != null) {
        nameView.setText(spot.name);
        addressView.setText(spot.address);
        typeView.setText(spot.type == 1 ? "Chinese Cuisine" : "Western Cuisine");    
    }
    if (restaurant != null) {
        contactView.setText(""+restaurant.contact);
    }

    return v;

}