2 daniel cf Daniel_cf 于 2012.12.26 15:36 提问

删除后android刷新Listview

在我的item删除后我想要刷新我的listview。但是好像不起作用,我是少了什么东西了么?我用了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));
    }

LogCat :

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)

3个回答

niangzhi
niangzhi   2012.12.26 16:01
已采纳
listAdapter.notifyDataSetChanged();

不起作用因为在你的adapter中你什么都没有。你没有在listAdapter中删除任何东西,所以什么都没有作用。改下onContextMenuItemSelected()成这样:

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

然后可以在deletefunc()试着用下listAdapter.remove(listAdapter.getItem(id))。然后应该可以自动调用notifyDataSetChanged();

Daniel_cf
Daniel_cf 非常感谢,可以了。
大约 5 年之前 回复
cytown
cytown   2012.12.26 15:47

代码逻辑很混乱啊,你要删除的是menuitem的id还是adapter的itemid?无论如何,你传入的是menuitem的id,而listAdapter.getItem(id)获得的是通过位置获得adatper的item。

Daniel_cf
Daniel_cf 谢谢你的回答,不过niangzhi的更详细一些。
大约 5 年之前 回复
Sueyexin
Sueyexin   2012.12.26 16:04

我没看见你是从adapters list删除element。你尅有调用listAdapter.remove(myFile),这个是可以删除element的,或者也可以调用notifyDataSetChanged()

Daniel_cf
Daniel_cf 谢谢,不过niangzhi的更详细一些。
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!