0

2 つのアクティビティ間でデータを転送し、アダプターを使用して表示しようとしています。以前、リストを表示するために同じアダプターを使用しましたが、これによりアダプターにいくつかのエラーが発生し、以下のコードでそれらを示しました。

誰でもそれを修正するために何をすべきかを理解できますか? 任意の助けをいただければ幸いです。

これらの詳細を渡す最初のアクティビティ、

                                intent.putExtra("quantity", quantity);
                                intent.putExtra("instructions", instructions);
                                intent.putExtra("subTotal", subTotal);
                                intent.putExtra("description", description);
                                intent.putExtra("price", price);

2回目の活動

        String quantity = getIntent().getStringExtra("quantity");
        String description = getIntent().getStringExtra("description");
        String subTotal = getIntent().getStringExtra("subTotal");
        String price = getIntent().getStringExtra("price");

        CustomGridPastaCart adapter = new CustomGridPastaCart(getApplicationContext(),
                quantity, description, subTotal, price); //error2

CustomGridPastaCart、BaseAdapter

public class CustomGridPastaCart extends BaseAdapter {
private Context context;
List<Item> items;

public CustomGridPastaCart(Context c, List<Item> items) {
    this.items = items;
}

public CustomGridPastaCart(Context applicationContext, String quantity, //it was forcing me to generate this error2 in above
        String description, String subTotal, String price) {
    // TODO Auto-generated constructor stub
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Item getItem(int position) { //here i had to change object to Item because it gave an error1 in below
    return items.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(
            R.layout.cart_activity, parent, false);
        holder.tvHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
        holder.tvUnitprice = (TextView) convertView
            .findViewById(R.id.lbl_unitprice);
        holder.tvQty = (TextView) convertView.findViewById(R.id.lbl_qty);
        holder.ivSubtotal = (TextView) convertView
            .findViewById(R.id.lblsubtotal);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Get item...
    Item item = getItem(position); //error1 here

    // Set values.
    holder.tvHeader.setText(item.description);
    holder.tvUnitprice.setText(item.price);
    holder.tvQty.setText(item.quantity);
    holder.ivSubtotal.setText(item.subTotal);

    return convertView;
}

private class ViewHolder {
   private TextView tvHeader;
   private TextView tvUnitprice;
   private TextView tvQty;
   private TextView ivSubtotal;

} }

アイテム.java

public class Item {
    String quantity;
    String description;
    String subTotal;
    String price;
}

ここに画像の説明を入力

4

1 に答える 1

0

アダプターの使い方が間違っています。ListView には、基本的にリスト (または配列) を含む構造体である Adapter と、それを処理して各項目を表すビューを作成するメソッドが必要です。

単一のアイテムのフィールドを渡しているためdescription、文字列であるため、これは失敗します。

@Override
public int getCount() {
    return description.size(); //The method size() is undefined for the type String
}

同じ問題がありますgetItem

@Override
public Object getItem(int position) { 
    return description.get(position); //The method get(int) is undefined for the type String
}

アダプターに次のようなものを実装する必要があります。

public class CustomGridPastaCart extends BaseAdapter {
    private Context context;
    List<Item> items;

    public CustomGridPastaCart(Context c, List<Item> items) {
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) { 
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                R.layout.cart_activity, parent, false);
            holder.tvHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
            holder.tvUnitprice = (TextView) convertView
                .findViewById(R.id.lbl_unitprice);
            holder.tvQty = (TextView) convertView.findViewById(R.id.lbl_qty);
            holder.ivSubtotal = (TextView) convertView
                .findViewById(R.id.lblsubtotal);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // Get item...
        Item item = getItem(position);

        // Set values.
        holder.tvHeader.setText(item.description);
        holder.tvUnitprice.setText(item.price);
        holder.tvQty.setText(item.quantity);
        holder.ivSubtotal.setText(String.valueOf(item.subTotal));

        return convertView;
    }

    private class ViewHolder {
       private TextView tvHeader;
       private TextView tvUnitprice;
       private TextView tvQty;
       private TextView ivSubtotal;
   }
}

そしてアイテムクラス:

public class Item {
    String quantity;
    String description;
    double subTotal;
    String price;
}

テスト:

String quantity = getIntent().getStringExtra("quantity");
String description = getIntent().getStringExtra("description");
String subTotal = getIntent().getStringExtra("subTotal");
String price = getIntent().getStringExtra("price");

Item item = new Item();
item.quantity = quantity;
item.description = description;
item.subTotal = subTotal;
item.price = price;

ArrayList<Item> list = new ArrayList<Item>();
list.add(item);

CustomGridPastaCart adapter = new CustomGridPastaCart(getApplicationContext(), list);
于 2015-02-17T12:51:21.180 に答える