2

我正在尝试在项目删除后刷新我的列表视图。但这似乎不起作用。我是不是错过了什么。我用 listAdapter.notifyDataSetChanged(); 也不工作。有人可以帮助我吗?谢谢

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getActivity().setContentView(R.layout.dl_listview);

        View empty = getActivity().findViewById(R.id.empty);
        DLListView = (ListView) getActivity().findViewById(R.id.DLListView);
        DLListView.setEmptyView(empty);

        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(getActivity(),
                    "Error! No SDCARD Found!",
                    Toast.LENGTH_LONG).show();
        } else {
            File directory = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "St");
            directory.mkdirs();
        }
        DLListView = (ListView) getActivity().findViewById(R.id.DLListView);

        myList = new ArrayList<String>();

        File sdCard = Environment.getExternalStorageDirectory();
        file = new File(sdCard.getAbsolutePath() + "/St/");

        File list[] = file.listFiles();

        for (int i = 0; i < list.length; i++) {
            String fileName = list[i].getName();
            fileName = fileName.replace(".txt", "");
            myList.add(fileName);

        }

        listAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.simplerow, myList);

        DLListView.setAdapter(listAdapter);

        DLListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                String textToPass = myList.get(position);
                Intent i = new Intent(getActivity(), ViewActivity.class);
                textToPass = textToPass.replace(textToPass + "", textToPass
                        + ".txt");
                i.putExtra("textToPass", textToPass);
                startActivity(i);
            }
        });

        DLListView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                registerForContextMenu(DLListView);
                return false;
            }
        });

    }

    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        String name = ((TextView) info.targetView).getText().toString();
        menu.setHeaderTitle(name);
        menu.add(0, v.getId(), 0, "Delete");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

            deletefunc(item.getItemId());

        return true;
    }



    public void deletefunc(int id) {

        String filenamec = fileName1;
        filenamec = filenamec.replace(filenamec + "", filenamec + ".txt");
        File folder = Environment.getExternalStorageDirectory();
        String fileName = folder.getPath() + "/St/" + filenamec;

        File myFile = new File(fileName);
        if (myFile.exists())
            myFile.delete();
        Toast.makeText(getActivity(), fileName1+" Deleted", Toast.LENGTH_SHORT)
        .show();
        listAdapter.remove(listAdapter.getItem(id));
    }

日志猫:

12-26 11:57:27.334: E/AndroidRuntime(5576): FATAL EXCEPTION: main
12-26 11:57:27.334: E/AndroidRuntime(5576): java.lang.IndexOutOfBoundsException: Invalid index 2131034154, size is 1
12-26 11:57:27.334: E/AndroidRuntime(5576):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at java.util.ArrayList.get(ArrayList.java:304)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at free.st.app.MyDownloads.deletefunc(MyDownloads.java:137)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at free.st.app.MyDownloads.onContextItemSelected(MyDownloads.java:118)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at android.support.v4.app.FragmentManagerImpl.dispatchContextItemSelected(FragmentManager.java:1933)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:360)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:200)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3691)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:929)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at android.widget.AbsListView.performItemClick(AbsListView.java:1185)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2713)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at android.widget.AbsListView$1.run(AbsListView.java:3468)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at android.os.Handler.handleCallback(Handler.java:605)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at android.os.Looper.loop(Looper.java:137)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at android.app.ActivityThread.main(ActivityThread.java:4512)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at java.lang.reflect.Method.invoke(Method.java:511)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
12-26 11:57:27.334: E/AndroidRuntime(5576):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

5
listAdapter.notifyDataSetChanged();

没有做任何事情是因为您没有对适配器做任何事情。您尚未从 listAdapter 中删除任何内容,因此无需更改任何内容。更改onContextMenuItemSelected()为这样(使其纯粹与列表位置一起使用。MenuItem ids 不是列表中的位置):

@Override
    public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    int position = info.position;
    deletefunc(position);
    return true;
    }

然后考虑在listAdapter.remove(listAdapter.getItem(id))某处做deletefunc()notifyDataSetChanged();无论如何,这应该会自动调用。

于 2012-12-26T03:54:20.020 回答
0

您似乎实际上并没有从适配器列表中删除该元素。你可以打电话listAdapter.remove(myFile),它会删除元素,并notifyDataSetChanged()为你打电话。

于 2012-12-26T03:54:32.810 回答
0

真的没有理由不在这里使用Loaders。您也不应该在 UI 线程上访问文件系统。更好的实现是使用AsyncTaskLoader并将您的文件访问权限移动到loadInBackground(). 您也可以通过扩展ListActivity而不是 Activity 来稍微清理一下。

于 2012-12-26T04:45:55.887 回答