Reputation: 638
Upon deletion of item from database, recycler view changes order of data after a one or two times of deleting and adding data to the list ,Deletion operation stop working or the Data in the list changes itself..
Activity which has the view and deletes from it
String[] items;
DbHandler db = new DbHandler(getApplicationContext());
items = db.get_item();
db.close();
listRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
scheduleAdapter = new ScheduleAdapter(getApplicationContext());
if (listRecyclerView != null)
listRecyclerView.setAdapter(scheduleAdapter);
listRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getApplicationContext()));
listRecyclerView.stopScroll();
listRecyclerView.addOnItemTouchListener(new RItemClickListener(getApplicationContext(), listRecyclerView, new RItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
}
@Override
public void onLongItemClick(View view,final int position) {
new AlertDialog.Builder(MySchedule.this)
.setIcon(android.R.drawable.alert_dark_frame)
.setTitle("Are you sure?")
.setMessage("Do you want to delete this note?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DbHandler db = new DbHandler(getApplicationContext());
db.delete_item(items[1]);
db.close();
scheduleAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No",null)
.show();
}
}));
}
@Override
protected void onResume(){
listRecyclerView.stopScroll();
super.onResume();
scheduleAdapter.notifyDataSetChanged();
}
My adapter class
public class ScheduleAdapter extends RecyclerView.Adapter<ScheduleAdapter.ViewHolder> {
public int Type_Text = 1;
Context r_context;
int pos = 1;
public ScheduleAdapter (Context context){
r_context=context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v; ViewHolder vItem = null;
if (viewType==Type_Text){
v= LayoutInflater.from(parent.getContext()).inflate(R.layout.typeview1,parent,false);
vItem = new ViewHolder(v,viewType,parent.getContext());
}
return vItem;
}
@Override
public void onBindViewHolder(final ViewHolder holder,final int position) {
String[] Text_name;
holder.setIsRecyclable(false);
setAnimation(holder.container);
pos = position;
if(holder.HolderId==1){
DbHandler db = new DbHandler(r_context);
Text_name = db.get_item();
holder.Text.setText(Text_name[position]);
db.close();
}
}
@Override
public int getItemCount() {
DbHandler db = new DbHandler(r_context);
int note_count = db.get_count();
db.close();
return note_count;
}
public int getPosition (){
return pos;
}
@Override
public int getItemViewType (int position){
return Type_Text;
}
private void setAnimation (View view){
Animation animation = AnimationUtils.loadAnimation(r_context, android.R.anim.slide_in_left);
view.startAnimation(animation);
}
// ViewHolder Class
public class ViewHolder extends RecyclerView.ViewHolder {
int HolderId; TextView Text;
CardView container;
public ViewHolder(View itemView, int View_Type, Context context) {
super(itemView);
container = (CardView) itemView.findViewById(R.id.type1_cardview);
if (View_Type==Type_Text){
Text = (TextView) itemView.findViewById(R.id.type1_text);
HolderId = 1;
}
My Recycler view click listener class
public class RItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mlistener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onLongItemClick(View view, int position);
}
GestureDetector mgesturedetector;
public RItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mlistener = listener;
mgesturedetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mlistener != null) {
mlistener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View childView = rv.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mlistener != null && mgesturedetector.onTouchEvent(e)) {
mlistener.onItemClick(childView, rv.getChildAdapterPosition(childView));
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { }
}
Somebody on stack overflow mentioned to implement this class to change the error this also didnt work
public class WrapContentLinearLayoutManager extends LinearLayoutManager {
public WrapContentLinearLayoutManager(Context context) {
super(context);
}
//... constructor
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
try {
super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) {
Log.e("Error", "IndexOutOfBoundsException in RecyclerView happens");
}
}
Kindly help! ...Thanks !
Upvotes: 1
Views: 74
Reputation: 638
scheduleAdapter.notifyItemRangeChanged(position,items.length);
Intent intent = getIntent();
finish();
startActivity(intent);
Adding these lines after scheduleAdapter.notifyItemRemoved(position) worked :)
Upvotes: 0