2

これは私がこれまでに持っているものです:

カスタム オブジェクト:

class ItemObject {
    List<String> name;
    List<String> total;
    List<String> rating;

public ItemObject(List<ItemObject> io) {
    this.total = total;
    this.name = name;
    this.rating = rating;
 }
}

アダプターへの呼び出し:

List<String> names, ratings, totals;

ItemObject[] io= new ItemObject[3];
io[0] = new ItemObject(names);
io[1] = new ItemObject(rating);
io[2] = new ItemObject(totals);

adapter = new ItemAdapter(Items.this, io);
setListAdapter(adapter);

上記が問題ないと仮定すると、私の質問は、コンストラクターである ItemAdapter をどのようにセットアップし、オブジェクトから 3 つのリストをアンラップするかということです。そして、getView で、次のものを割り当てます。

一致する各位置は次のとおりです。

    TextView t1 = (TextView) rowView.findViewById(R.id.itemName);
    TextView t2 = (TextView) rowView.findViewById(R.id.itemTotal);
    RatingBar r1 = (RatingBar) rowView.findViewById(R.id.ratingBarSmall);

たとえば、配列「names」の位置 0 を t1 にします。配列「totals」の 0 を t1 に配置します。配列「ratings」の 0 を r1 に配置します。

編集: 誰かにアダプター全体を書いてほしくありません。データを使用できるように、カスタム オブジェクトからリストをアンラップする方法を知る必要があるだけです。(別の質問で取り上げられていない、または尋ねられていないもの

4

1 に答える 1

11

コードは実際の形式では機能しません。にデータのリストが本当に必要ItemObjectですか? 私の推測ではノーですItemObject。行レイアウトの 3 つのビューに対応する 3 つの文字列を保持する が必要なだけです。このような場合は:

class ItemObject {
    String name;
    String total;
    String rating;// are you sure this isn't a float

public ItemObject(String total, String name, String rating) {
    this.total = total;
    this.name = name;
    this.rating = rating;
 }
}

次に、リストは次のリストにマージされますItemObject:

List<String> names, ratings, totals;
ItemObject[] io= new ItemObject[3];
// use a for loop
io[0] = new ItemObject(totals.get(0), names.get(0), ratings(0));
io[1] = new ItemObject(totals.get(1), names.get(1), ratings(1));
io[2] = new ItemObject(totals.get(2), names.get(2), ratings(2));
adapter = new ItemAdapter(Items.this, io);
setListAdapter(adapter);

そしてアダプタークラス:

public class ItemAdapter extends ArrayAdapter<ItemObject> {

        public ItemAdapter(Context context,
                ItemObject[] objects) {
            super(context, 0, objects);         
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // do the normal stuff
            ItemObject obj = getItem(position);
            // set the text obtained from obj
                    String name = obj.name; //etc       
                    // ...

        }       

}
于 2012-07-21T16:09:58.707 に答える