我尝试在两个活动之间传输数据,然后使用适配器显示。之前我使用相同的适配器来显示List,但这会在我的适配器中出现几个错误,并且我已在下面的代码中显示它们。
任何人都可以知道我应该怎么做才能纠正它。任何帮助将不胜感激。
第一项活动传递了这些细节,
intent.putExtra("quantity", quantity);
intent.putExtra("instructions", instructions);
intent.putExtra("subTotal", subTotal);
intent.putExtra("description", description);
intent.putExtra("price", price);
第二项活动
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;
} }
Item.java
public class Item {
String quantity;
String description;
String subTotal;
String price;
}
答案 0 :(得分: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;
}
}
和Item类:
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);