2012-12-26 55 views
2

我試圖在刪除項目後刷新我的列表視圖。但它似乎並不奏效。我錯過了什麼。我用listAdapter.notifyDataSetChanged();不工作。有人可以幫我嗎?謝謝刪除後的Android刷新列表視圖

@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) 

回答

5
listAdapter.notifyDataSetChanged(); 

,因爲你還沒有對您的適配器什麼沒有做任何事情。你還沒有從listAdapter中刪除任何東西,所以沒有什麼可以改變的。更改onContextMenuItemSelected()所以它是這樣的(使其與列表中的位置的作品純粹是菜單項的ID不在列表中的位置。):

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

+0

試過這個,但我得到了這個錯誤代碼「ArrayAdapter 類型中的方法remove(String)不適用於參數()」 –

+0

找到一個錯誤。請檢查我的logcat –

+0

是的,我意識到這一點,然後更新我的答案相應:) –

0

您似乎沒有實際從適配器列表中刪除該元素。您可以撥打listAdapter.remove(myFile),它將刪除該元素,併爲您撥打notifyDataSetChanged()