我正在使用Recycler View来显示共享首选项ID中的项目。我必须删除Recyler View项目以及SharedPreference商店项目。 但是当我试图删除Recycler View的项目时,我收到了错误。
我的适配器:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import com.journaldev.navigationdrawer.ActionBarIconProcess.Cart;
import com.journaldev.navigationdrawer.ActionBarIconProcess.CartItems;
import com.journaldev.navigationdrawer.ActionBarIconProcess.Wishlist;
import com.journaldev.navigationdrawer.Activity.singleshooppingcart;
import com.journaldev.navigationdrawer.Constants.Constants;
import com.journaldev.navigationdrawer.Model.CartItemoriginal;
import com.journaldev.navigationdrawer.Model.CartitemModel;
import com.journaldev.navigationdrawer.Model.Cartitemoringinaltwo;
import com.journaldev.navigationdrawer.R;
import com.journaldev.navigationdrawer.app.YelloPage;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import org.json.JSONArray;
import org.json.JSONException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
/**
* Created by admin on 6/2/2016.
*/
public class CartlistAdapter extends RecyclerView.Adapter < CartlistAdapter.ViewHolder > {
private ArrayList < CartItemoriginal > cartlistadp;
private ArrayList < Cartitemoringinaltwo > cartlistadp2;
DisplayImageOptions options;
private Context context;
public static final String MYCARTPREFERENCE = "CartPrefs";
SharedPreferences.Editor editor;
SharedPreferences shared;
String pos;
String date;
String currentDateandTime;
private static final int VIEW_TYPE_ONE = 1;
private static final int VIEW_TYPE_TWO = 2;
private static final int TYPE_HEADER = 0;
private Double orderTotal = 0.00;
DecimalFormat df = new DecimalFormat("0");
Double extPrice;
public CartlistAdapter(ArrayList < CartItemoriginal > cartlistadp, ArrayList < Cartitemoringinaltwo > cartlistadp2, Context context) {
this.cartlistadp = cartlistadp;
this.cartlistadp2 = cartlistadp2;
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("Rs" + " " + cartlistadp.get(position).getPrice());
viewHolder.cartdelivery.setText(cartlistadp2.get(position).getDelivery());
viewHolder.cartshippin.setText(cartlistadp2.get(position).getShippincharge());
viewHolder.cartsellername.setText(cartlistadp2.get(position).getSellername());
viewHolder.Error.setText(cartlistadp2.get(position).getError());
viewHolder.qty.setTag(cartlistadp.get(position));
if (cartlistadp.get(position).getQty() != 0) {
viewHolder.qty.setText(String.valueOf(cartlistadp.get(position).getQty()));
} else {
viewHolder.qty.setText("0");
}
// 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() {
int it = 0;
if (cartlistadp.equals(cartlistadp)) {
it = cartlistadp.size();
}
if (cartlistadp2.equals(cartlistadp2)) {
it = cartlistadp2.size();
}
return it;
}
public long getItemId(int position) {
return position;
}
public Object getItem(int position) {
return cartlistadp2.get(position);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView productnames, cartalisname, cartprice, cartdelivery, cartshippin, cartsellername, Error, total;
private ImageView cartitemimg;
private ImageButton wishbtn, removebtn;
private LinearLayout removecart, movewishlist;
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);
Error = (TextView) view.findViewById(R.id.error);
removecart = (LinearLayout) view.findViewById(R.id.removecart);
movewishlist = (LinearLayout) view.findViewById(R.id.movewishlist);
Error = (TextView) view.findViewById(R.id.error);
qty = (EditText) view.findViewById(R.id.quantity);
qty.addTextChangedListener(new MyTextWatcher(view));
qty.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
qty.setSelection(qty.getText().length());
return false;
}
});
shared = view.getContext().getSharedPreferences(MYCARTPREFERENCE, context.MODE_PRIVATE);
editor = view.getContext().getSharedPreferences(MYCARTPREFERENCE, context.MODE_PRIVATE).edit();
cd = (CardView) view.findViewById(R.id.cv);
productnames.setSingleLine(false);
productnames.setEllipsize(TextUtils.TruncateAt.END);
productnames.setMaxLines(2);
totalPrice();
// view.setClickable(true);
// view.setFocusableInTouchMode(true);
removecart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cartlistadp.size() == 1) {
Intent list = new Intent(v.getContext(), Cart.class);
context.startActivity(list);
((Activity) context).finish();
removeAt(getPosition());
Log.i(String.valueOf(getPosition()), "item");
Toast.makeText(context, "All items deleted from your WishList", Toast.LENGTH_LONG).show();
} else {
removeAt(getPosition());
}
}
});
错误:
: 30: 03.108 2118 - 2118 / com.journaldev.navigationdrawer E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.journaldev.navigationdrawer, PID: 2118
java.lang.IndexOutOfBoundsException: Inconsistency detected.Invalid item position 1(offset: -1).state: 2
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java: 4320)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java: 4278)
at com.journaldev.navigationdrawer.Adapter.CustomLinearLayoutManagercartpage.measureScrapChild(CustomLinearLayoutManagercartpage.java: 67)
at com.journaldev.navigationdrawer.Adapter.CustomLinearLayoutManagercartpage.onMeasure(CustomLinearLayoutManagercartpage.java: 38)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java: 2343)
at android.view.View.measure(View.java: 17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java: 1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java: 722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java: 613)
at android.view.View.measure(View.java: 17547)
at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java: 1415)
at android.widget.FrameLayout.onMeasure(FrameLayout.java: 436)
at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java: 480)
at android.view.View.measure(View.java: 17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java: 1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java: 722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java: 613)
at android.view.View.measure(View.java: 17547)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java: 727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java: 463)
at android.view.View.measure(View.java: 17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java: 436)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java: 124)
at android.view.View.measure(View.java: 17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java: 1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java: 722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java: 613)
at android.view.View.measure(View.java: 17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java: 436)
at android.view.View.measure(View.java: 17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java: 1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java: 722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java: 613)
at android.view.View.measure(View.java: 17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java: 436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java: 2615)
at android.view.View.measure(View.java: 17547)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java: 2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java: 1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java: 1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java: 1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java: 5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java: 767)
at android.view.Choreographer.doCallbacks(Choreographer.java: 580)
at android.view.Choreographer.doFrame(Choreographer.java: 550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java: 753)
at android.os.Handler.handleCallback(Handler.java: 739)
at android.os.Handler.dispa
06 - 09 08: 30: 53.097 2118 - 2118 / com.journaldev.navigationdrawer I / Process: Sending signal.PID: 2118 SIG:
如何解决这个问题我使用两个模型作为一个适配器可能是因为我收到这样的错误。 在此先感谢。
答案 0 :(得分:1)
在调用getAdapterPosition()
getPosition
而不是removeAt
答案 1 :(得分:0)
在你的getItem(int position)上,你将返回你的cartlistadp2(arraylist2)。
我不知道下面的代码在getItemCount()中实际执行了什么,但也将此代码用于getItem()
if (cartlistadp.equals(cartlistadp)) {
return cartlistadp.get(position);
}
if (cartlistadp2.equals(cartlistadp2)) {
return cartlistadp2.get(position);
}