2014-03-31 34 views
0

有两个对象:填写两个不同的对象到一个单一的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; 
} 

我有每个对象的两个数组列表,我想填充到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软弱,我怎么可以改变的构造?

+0

您将所有字段公开,并且您没有Spot的构造函数。你为什么认为你会需要一个餐厅?你可以像这样创建你的点:'Spot s = new Spot();'然后分配所有字段。对派生的'Restaurant'使用相同的方法,不需要显式构造函数。 – kiruwka

回答

1

这不是一个真正的android问题。

只需让您的RestaurantSpot的子类型,并且具有单个的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; 

} 
+0

这是更可读的解决方案。感谢您的帮助 – user782104

+0

请查看更新问题 – user782104

+0

您问如何创建适配器? – kiruwka

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; 

这是不是唯一的解决方案,但它会奏效。

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; 

} 
+0

如何更新对象的构造函数?谢谢 – user782104

+0

对不起,这是一个错字,我只是更新了我的答案。 –

+1

@ user782104我根据您编辑的问题更新了代码。 –

相关问题