我正在使用Recycler View来显示该项目,但我使用了两个请求URL来通过volley获取数据。 我尝试过但效果不好,无法通过排球获得第二次获取JSON URL的数据。
模型:
public class CartItemoriginal {
public String productimg;
public String productname;
public String aliasname;
public int qty;
public String price;
public String delivery;
public String shippincharge;
public String sellername;
public String productid;
private int Quantity;
public CartItemoriginal(String productimg, String productname, String aliasname, int qty, String price, String delivery, String shippincharge, String sellername, String productid) {
this.productimg = productimg;
this.productname = productname;
this.aliasname = aliasname;
this.qty = qty;
this.price = price;
this.delivery = delivery;
this.shippincharge = shippincharge;
this.sellername = sellername;
this.productid = productid;
}
public CartItemoriginal(String productimg, String productname, String aliasname, int qty, String price, String productid) {
this.productimg = productimg;
this.productname = productname;
this.aliasname = aliasname;
this.qty = qty;
this.price = price;
this.productid = productid;
}
public CartItemoriginal(String delivery, String shippincharge, String sellername) {
this.delivery = delivery;
this.shippincharge = shippincharge;
this.sellername = sellername;
}
public int increaseQuantity(int quantity) {
if (quantity < 1)
quantity = 1;
else
quantity++;
Quantity = quantity;
return Quantity;
}
public String getProductimg() {
return productimg;
}
public void setProductimg(String productimg) {
this.productimg = productimg;
}
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public String getAliasname() {
return aliasname;
}
public void setAliasname(String aliasname) {
this.aliasname = aliasname;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getDelivery() {
return delivery;
}
public void setDelivery(String delivery) {
this.delivery = delivery;
}
public String getShippincharge() {
return shippincharge;
}
public void setShippincharge(String shippincharge) {
this.shippincharge = shippincharge;
}
public String getSellername() {
return sellername;
}
public void setSellername(String sellername) {
this.sellername = sellername;
}
public String getProductid() {
return productid;
}
public void setProductid(String productid) {
this.productid = productid;
}
}
适配器:
public class CartlistAdapter extends RecyclerView.Adapter < CartlistAdapter.ViewHolder > {
private ArrayList < CartItemoriginal > cartlistadp;
DisplayImageOptions options;
private Context context;
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences.Editor editor;
SharedPreferences shared;
String pos;
private static final int VIEW_TYPE_ONE = 1;
private static final int VIEW_TYPE_TWO = 2;
public CartlistAdapter(ArrayList < CartItemoriginal > cartlistadp, Context context) {
this.cartlistadp = cartlistadp;
this.context = context;
options = new DisplayImageOptions.Builder().cacheOnDisk(true).cacheInMemory(true).showImageOnLoading(R.drawable.b2)
.showImageForEmptyUri(R.drawable.b2).build();
if (YelloPage.imageLoader.isInited()) {
YelloPage.imageLoader.destroy();
}
YelloPage.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
}
public CartlistAdapter() {
}
public int getItemViewType(int position) {
if (cartlistadp.size() == 0) {
Toast.makeText(context, String.valueOf(cartlistadp), Toast.LENGTH_LONG).show();
return VIEW_TYPE_TWO;
}
return VIEW_TYPE_ONE;
}
@Override
public CartlistAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
ViewHolder viewHolder = null;
switch (position) {
case VIEW_TYPE_TWO:
View view2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_cart, viewGroup, false);
viewHolder = new ViewHolder(view2);
// return view holder for your placeholder
break;
case VIEW_TYPE_ONE:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cartitemrow, viewGroup, false);
viewHolder = new ViewHolder(view);
// return view holder for your normal list item
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(CartlistAdapter.ViewHolder viewHolder, int position) {
viewHolder.productnames.setText(cartlistadp.get(position).getProductname());
viewHolder.cartalisname.setText(cartlistadp.get(position).getAliasname());
viewHolder.cartprice.setText(cartlistadp.get(position).getPrice());
viewHolder.cartdelivery.setText(cartlistadp.get(position).getDelivery());
viewHolder.cartshippin.setText(cartlistadp.get(position).getShippincharge() + "%" + " " + "OFF");
viewHolder.cartsellername.setText(cartlistadp.get(position).getSellername());
// viewHolder.qty.setText(cartlistadp.get(position).getQty());
// viewHolder.wishrating.setText(WishListadp.get(i).getCartitemname());
YelloPage.imageLoader.displayImage(cartlistadp.get(position).getProductimg(), viewHolder.cartitemimg, options);
}
@Override
public int getItemCount() {
return cartlistadp.size();
}
public long getItemId(int position) {
return position;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView productnames, cartalisname, cartprice, cartdelivery, cartshippin, cartsellername;
private ImageView cartitemimg;
private ImageButton wishbtn, removebtn;
private CardView cd;
private EditText qty;
public ViewHolder(final View view) {
super(view);
productnames = (TextView) view.findViewById(R.id.cartitemname);
cartalisname = (TextView) view.findViewById(R.id.cartalias);
cartprice = (TextView) view.findViewById(R.id.CartAmt);
cartdelivery = (TextView) view.findViewById(R.id.cartdel);
cartshippin = (TextView) view.findViewById(R.id.shippingcrg);
cartsellername = (TextView) view.findViewById(R.id.cartSellerName);
cartitemimg = (ImageView) view.findViewById(R.id.cartimg);
qty = (EditText) view.findViewById(R.id.quantity);
cd = (CardView) view.findViewById(R.id.cv);
productnames.setSingleLine(false);
productnames.setEllipsize(TextUtils.TruncateAt.END);
productnames.setMaxLines(2);
}
}
}
代码:
public void firstServiceCall(String list) {
url = Constants.singleproducturl + list;
showpDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.GET, url, null,
new Response.Listener < JSONObject > () {
@Override
public void onResponse(JSONObject response) {
if (response != null) {
// int status=jsonObject.optInt("status");
String status = response.optString("status");
if (status.equalsIgnoreCase("200")) { //check the status 200 or not
try {
productpath = response.getString("productPath");
} catch (JSONException e) {
e.printStackTrace();
}
try {
JSONObject responses = response.getJSONObject("response");
jsonarray = responses.getJSONArray(DATA);
if (jsonarray.length() > 0) {
// looping through json and adding to movies list
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject product = jsonarray.getJSONObject(i);
cartpid = product.getString("product_id");
cartproductname = product.getString("product_name");
cartaliasname = product.getString("product_alias");
cartprice = product.getString("mrp_price");
sellerid = product.getString("seller_id");
JSONArray pimg = product.getJSONArray("product_images");
JSONObject firstimg = pimg.getJSONObject(0);
cartimg = firstimg.getString("original_res");
String[] img2 = cartimg.split("\\.");
String imagone = productpath + sellerid + '/' + img2[0] + '(' + '2' + '0' + '0' + ')' + '.' + img2[1];
int items = 2;
cart.add(new CartItemoriginal(imagone, cartproductname, cartaliasname, items, cartprice, cartpid));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
pincodecheck();
// cartadapter.notifyDataSetChanged();
// stopping swipe refresh
// swipeRefreshLayout.setRefreshing(false);
} // condtion check the status 200
else // this is if status falied in runtime
{
Toast.makeText(CartItems.this, "Status Failed in Banner Page check ur network connection", Toast.LENGTH_LONG).show();
}
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
/***************************************** second url volley ********************************************/
public void pincodecheck() {
if (!pincode.isEmpty()) {
String pincon = pincode;
int pin4 = Integer.parseInt(pincon);
secondServiceCall(pin4, cartpid, sellerid);
} else {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}
}
public void secondServiceCall(int pin, String productid, String sellerid) {
String pinurl = "http://192.168.0.33/sharpswebsite3/qcrest1.0/?type=pinCodeCheck&result=json&product_id=" + productid + "&seller_id=" + sellerid + "&pinCode=" + pin;
// use this var membershipid acc to your need ...
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.GET, pinurl, null,
new Response.Listener < JSONObject > () {
@Override
public void onResponse(JSONObject response) {
if (response != null) {
// int status=jsonObject.optInt("status");
String status = response.optString("status");
if (status.equalsIgnoreCase("200")) { //check the status 200 or not
try {
JSONObject responses = response.getJSONObject("response");
jsonarray = responses.getJSONArray(DATA);
if (jsonarray.length() > 0) {
// looping through json and adding to movies list
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject product = jsonarray.getJSONObject(i);
process = product.getString("process");
Message = product.getString("message");
if (process.equalsIgnoreCase("success") && Message.equalsIgnoreCase("success")) {
cartdelivery = product.getString("delivery");
cartshippingcharge = product.getString("shipping_charge");
String pincode = product.getString("pincode");
cart.add(new CartItemoriginal(cartdelivery, cartshippingcharge, "richard feloboune"));
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
cartadapter.notifyDataSetChanged();
// stopping swipe refresh
// swipeRefreshLayout.setRefreshing(false);
} // condtion check the status 200
else // this is if status falied in runtime
{
Toast.makeText(CartItems.this, "Status Failed in Banner Page check ur network connection", Toast.LENGTH_LONG).show();
}
}
recyleitems = (RecyclerView) findViewById(R.id.mycartitem);
llm = new LinearLayoutManager(CartItems.this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyleitems.setLayoutManager(llm);
recyleitems.setHasFixedSize(true);
cartadapter = new CartlistAdapter(cart, CartItems.this);
Log.i(String.valueOf(cartadapter), "cartadapter");
recyleitems.setAdapter(cartadapter);
cartadapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
我可以先得到urloutput而不是第二个。
如何解决此错误,请提前感谢。
答案 0 :(得分:0)
尝试在第一次通话后调用第二项服务后添加此项
recyleitems = (RecyclerView) findViewById(R.id.mycartitem);
llm = new LinearLayoutManager(CartItems.this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyleitems.setLayoutManager(llm);
recyleitems.setHasFixedSize(true);
cartadapter = new CartlistAdapter(cart, CartItems.this);
Log.i(String.valueOf(cartadapter), "cartadapter");
recyleitems.setAdapter(cartadapter);
cartadapter.notifyDataSetChanged();
更好的方法是在onCreate
中声明您的观点,并且在调用服务后,只需在自定义ArrayList
中添加数据,并在成功添加数据List
后调用{{1} }}