因此,我尝试通过onClickListener将来自我的自定义适配器的Intent发送到另一个活动,以启动碎片,但是每次这样做,都会出现以下错误: java.lang.RuntimeException:包裹:无法封送值。
这是自定义适配器代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.view_order_list_item, parent, false);
}
TextView orderID = convertView.findViewById(R.id.listOrderID);
TextView status = convertView.findViewById(R.id.listStatus);
TextView datePlaced = convertView.findViewById(R.id.listDate);
TextView amount = convertView.findViewById(R.id.listAmount);
ImageButton cancelOrderBtn = convertView.findViewById(R.id.cancelOrderListBtn);
ImageButton complaintBtn = convertView.findViewById(R.id.complaintOrderListBtn);
orderID.setText(arr.get(position).getOrderID());
status.setText(arr.get(position).getStatus());
datePlaced.setText(arr.get(position).getDatePlaced());
amount.setText(arr.get(position).getAmountTotal().toString());
cancelOrderBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder cancelO = new AlertDialog.Builder(context);
cancelO.setMessage("Do you want to cancel this order?").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
cancelOrder(orderID.getText().toString());
return;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = cancelO.create();
alert.setTitle("Cancel order?");
alert.show();
}
});
complaintBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder complaintO = new AlertDialog.Builder(context);
complaintO.setMessage("Submit a complaint for this order?").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i=new Intent(context, HomeActivity.class);
i.putExtra("key","value");
context.startActivity(i);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = complaintO.create();
alert.setTitle("Cancel order?");
alert.show();
}
});
return convertView;
}
这是订单对象代码
public class RetrieveOrderObject {
String orderID;
String status;
Double amountTotal;
String datePlaced;
public RetrieveOrderObject(String orderID, String status, Double amountTotal, String datePlaced) {
this.orderID = orderID;
this.status = status;
this.amountTotal = amountTotal;
this.datePlaced = datePlaced;
}
public String getOrderID() {
return orderID;
}
public void setOrderID(String orderID) {
this.orderID = orderID;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Double getAmountTotal() { return amountTotal; }
public void setAmountTotal(Double amountTotal) {
this.amountTotal = amountTotal;
}
public String getDatePlaced() {
return datePlaced;
}
public void setDatePlaced(String datePlaced) {
this.datePlaced = datePlaced;
}
}
这是显示列表视图的片段
public class ViewOrdersFragment extends Fragment {
ListView simpleList;
String username;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.view_order_list, container, false);
ArrayList<RetrieveOrderObject> order = (ArrayList<RetrieveOrderObject>) getArguments().getSerializable("order");
ViewOrderListCustomAdaptor adaptor = new ViewOrderListCustomAdaptor(getContext(), order);
simpleList = view.findViewById(R.id.listView);
simpleList.setAdapter(adaptor);
return view;
}
@Override
public void onStart() {
super.onStart();
findUser();
}
public void findUser() {
username = User.USERNAME;
}
我在做什么错?我不在乎该视图,因为下次调用该视图时会重新创建它。我只想调用新片段。
顺便说一句,自定义适配器已连接到另一个不同的片段。不知道这是否重要。