Recycler View适配器管理列表不正确

时间:2017-03-31 12:48:39

标签: android database listview android-recyclerview

从数据库中删除项目后,回收站视图在删除一次或两次数据并将数据添加到列表后更改数据顺序,删除操作停止工作或列表中的数据自行更改..

具有查看和删除的活动

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();
}

我的适配器类

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;
        }

我的Recycler视图单击侦听器类

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) { }
}

有人提到堆栈溢出实现此类来更改错误,这也没有用

 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");
    }
}

请帮忙! ...谢谢 !

1 个答案:

答案 0 :(得分:0)

scheduleAdapter.notifyItemRangeChanged(position,items.length);
                        Intent intent = getIntent();
                        finish();
                        startActivity(intent);

在scheduleAdapter.notifyItemRemoved(position)之后添加这些行 工作:))