重命名的文件未显示在RecyclerView中,而是使应用程序崩溃

时间:2016-09-13 08:57:24

标签: android android-recyclerview

我第一次使用RecyclerView,我正在尝试重命名视图中的文件。文件被重命名,但回收器不会自行更新。我还实现了MediaScanner但它崩溃了应用程序,但是重启后列表会更新为重命名的文件。有没有办法在不调用媒体扫描仪的情况下更新列表?

特定重命名代码:

holder.renamebutton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("Title");

// Set up the input
        final EditText input = new EditText(mContext);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
        input.setInputType(InputType.TYPE_CLASS_TEXT );
        builder.setView(input);

// Set up the buttons
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                m_Text = input.getText().toString();
                File mydir =new File( Environment.getExternalStorageDirectory()+"/FreeSoundRecorder/");
                if(mydir.exists()){
                    File from = new File(mydir,music.getTitle()+".mp3");
                    File to = new File(mydir,m_Text+".mp3");
                    if(from.exists()) {
                        from.renameTo(to);
                        String filePath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/FreeSoundRecorder/"+ m_Text+".mp3";
    MediaScannerConnection.scanFile(
            mContext,
            new String[]{filePath},
            null,
            new MediaScannerConnection.OnScanCompletedListener() {
                @Override
                public void onScanCompleted(String path, Uri uri) {
                    Log.v("MehnatSafal",
                            "file " + path + " was scanned seccessfully: " + uri);

                }
            });
    mContext.getContentResolver().delete(trackuri, null,null);

    notifyItemRemoved(position);
    notifyItemRangeChanged(position,mMusic.size());

                    }
                }
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();
}

logcat的:

09-13 14:33:49.270 16453-16453/cafe.adriel.androidaudiorecorder.example E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                          Process: cafe.adriel.androidaudiorecorder.example, PID: 16453
                                                                                          java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{42450820 position=7 id=-1, oldPos=1, pLpos:1 scrap [attachedScrap] tmpDetached no parent}
                                                                                              at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4251)
                                                                                              at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4382)
                                                                                              at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
                                                                                              at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
                                                                                              at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
                                                                                              at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
                                                                                              at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
                                                                                              at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2864)
                                                                                              at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
                                                                                              at android.view.View.layout(View.java:14828)
                                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                                                                                              at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
                                                                                              at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
                                                                                              at android.view.View.layout(View.java:14828)
                                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                                              at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                                              at android.view.View.layout(View.java:14828)
                                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                              at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
                                                                                              at android.view.View.layout(View.java:14828)
                                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                                              at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                                              at android.view.View.layout(View.java:14828)
                                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                                                                                              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                                                                                              at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                                                                                              at android.view.View.layout(View.java:14828)
                                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                                              at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                                              at android.view.View.layout(View.java:14828)
                                                                                              at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                              at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2033)
                                                                                              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1790)
                                                                                              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1046)
                                                                                              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5655)
                                                                                              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                                                                                              at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                                                              at android.view.Choreographer.doFrame(Choreographer.java:544)
                                                                                              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                                                                                              at android.os.Handler.handleCallback(Handler.java:733)
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                              at android.os.Looper.loop(Looper.java:136)
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5052)
                                                                                              at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                              at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
                                                                                              at dalvik.system.NativeStart.main(Native Method)

那么可能的解决方案是什么?提前谢谢。

0 个答案:

没有答案