当光标改变时,带有onlongpress问题的Customcursorrecylerviewadapter

时间:2015-10-19 10:34:37

标签: android cursor android-recyclerview android-cursoradapter

我正在使用customCursorRecyclerViewAdpater并实现长按自定义类RecyclerTouchListner implements RecyclerView.OnItemTouchListener

GestureDetector 我正在 onLongPress 从那我得到光标 _id (databaseId)

但是,当我在片段端更改光标(使用Where cond。)并设置customCursorRecyclerViewAdpater时,它会显示数据。

问题是onlongclick方法有旧游标,这就是 CursorIndexOutOfBoundsException 错误发生的原因。

这是我的代码。

RecyclerTouchListner.java

 public class RecyclerTouchListner implements RecyclerView.OnItemTouchListener {

    private GestureDetector gestureDetector;
    private ClickListner clickListner;

    public RecyclerTouchListner(Context context, final RecyclerView mrecyclerView, final ClickListner clickListner,  final Cursor cursor) {
        Log.d("", "constructor..................");


        this.clickListner = clickListner;

        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
//                Log.d("===========id   ", cursor.getString(cursor.getColumnIndex("_id")));
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = mrecyclerView.findChildViewUnder(e.getX(), e.getY());
                cursor.moveToPosition(mrecyclerView.getChildAdapterPosition(child));


                if (child != null && clickListner != null) {
/*errorororororo   line..................................*/
                    clickListner.onLongClick(child, mrecyclerView.getChildAdapterPosition(child), cursor.getInt(cursor.getColumnIndex("_id")));

                }

            }
        });
    }


    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        View child = rv.findChildViewUnder(e.getX(), e.getY());

        if (child != null && clickListner != null && gestureDetector.onTouchEvent(e)) {
            clickListner.onClick(child, rv.getChildAdapterPosition(child));
        }



        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }


    public static interface ClickListner {


        public void onClick(View view, int Position);

        public void onLongClick(View view, int Position, int id);
    }
}

错误

 FATAL EXCEPTION: main
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime: Process: com.serpentcs.kookdokoo, PID: 28650
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at com.serpentcs.kookdokoo.notiRecycle.RecyclerTouchListner$1.onLongPress(RecyclerTouchListner.java:40)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at android.view.GestureDetector.dispatchLongPress(GestureDetector.java:690)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at android.view.GestureDetector.access$200(GestureDetector.java:37)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at android.view.GestureDetector$GestureHandler.handleMessage(GestureDetector.java:266)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-19 05:43:21.404 28650-28650/com.serpentcs.kookdokoo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Fragment.java

它有方法getdata();

private void getdata(String Selection, String[] SeleArgs) {

        String[] columns = {"_id,title,summary"};
        final Cursor c = db.query("content", columns, Selection, SeleArgs, null, null,
                "_id DESC");
        Log.e("", "============" + c.getCount());

        mAdapter = new SimpleCursorRecyclerAdapter(getActivity(), R.layout.recyclerformate, c);


        rv.setAdapter(mAdapter);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.addOnItemTouchListener(new RecyclerTouchListner(getActivity(), rv, new RecyclerTouchListner.ClickListner() {
            @Override
            public void onClick(View view, int Position) {
                Toast.makeText(getActivity(), "onclick.." + Position, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onLongClick(View view, final int Position, final int idofDatabase) {


              Toast.makeText(getActivity(), "onLongClick.." + idofDatabase, Toast.LENGTH_LONG).show();

            }


        }, c));
    }

我在3个不同的时间调用这个方法

(1)getdata(null,null)假设它在recylerview中提供 10 行数据库

(2)getdata(" somefild =?",new string [] {value});假设它有2行

(3)getdata(null,null)现在当我onlongclick 4th 行时,它会在日志中显示错误。

是最终关键字的问题吗? 或其他

请帮助..

提前致谢,抱歉我的英语不好。

0 个答案:

没有答案