2014-03-31 71 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我根據您編輯的問題更新了代碼。 –

相關問題