2013-06-04 71 views
0

我在Android上遇到ListView问题。其实不是ListView,因为它是在Sherlock Fragment上实现的。所以特别是: 我有2个班,一个是Fragment,一个是正常的ActivityActivity从网上下载一个文件,在Fragment中有一个包含下载文件的列表。
这是我对Fragment适配器:自定义ListView不刷新

File mydownload = new File (Environment.getExternalStorageDirectory()+ "/Sample Folder"); 
int i = 0; 
while (i < mydownload.list().length) { 
    adapter.add(mydownload.list()[i]); 
    i++; 
} 
setListAdapter(adapter); 

所以适配器捕获所有的文件示例文件夹,并把它在名单上。当我下载一个文件时,适配器不刷新列表(我认为它是在不同的上下文中,但我不知道解决方案)。刷新我必须关闭并打开应用程序。我尝试了很多可能的解决方案,但没有任何帮助。

编辑:我Fragment代码:

public class AppleFragment extends SherlockListFragment{  
/** An array of items to display in ArrayList */ 
private static ArrayAdapter<String> adapter = null; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
    /** Creating array adapter to set data in listview */ 

    adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), R.layout.liststyle, new ArrayList<String>()); 
    View v = super.onCreateView(inflater, container, savedInstanceState); 
    /** Setting the array adapter to the listview */ 
    File mydownload = new File (Environment.getExternalStorageDirectory()+ "/Gazzetta Ufficiale"); 
    int i = 0; 
    while (i < mydownload.list().length) { 
     adapter.add(mydownload.list()[i]); 
     i++; 
    } 
    setListAdapter(adapter); 
    return v; 

}  

@Override 
public void onStart() {  
    super.onStart(); 
    getListView().setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> adapter, View view, int position, long id){ 
      String gazza = (String) adapter.getItemAtPosition(position); 
      File pdf = new File (Environment.getExternalStorageDirectory()+ "/Gazzetta Ufficiale" + "/" + gazza); 
      try { 
       Uri path = Uri.fromFile(pdf); 
       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setDataAndType(path, "application/pdf"); 
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 



     } 
    }); 

    /** Setting the multiselect choice mode for the listview */ 
    //getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() { 

     public boolean onItemLongClick (AdapterView<?> arg0, View view, int position, long id){ 
      final int pos = position; 
      String gazza = (String) arg0.getItemAtPosition(position); 
      final File pdf = new File (Environment.getExternalStorageDirectory()+ "/Gazzetta Ufficiale" + "/" + gazza); 
      DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        switch (which){ 
        case DialogInterface.BUTTON_POSITIVE: 
         //Yes button clicked 
         pdf.delete(); 
         adapter.remove(adapter.getItem(pos)); 
         adapter.notifyDataSetChanged(); 
         break; 

        case DialogInterface.BUTTON_NEGATIVE: 
         //No button clicked 
         dialog.dismiss(); 
         break; 
        } 
       } 
      }; 

      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
      builder.setMessage("Vuoi eliminare la Gazzetta?").setPositiveButton("Elimina", dialogClickListener) 
       .setNegativeButton("Annulla", dialogClickListener).show(); 
      return true; 
     } 
    }); 
} 
} 

下载活动:在你的适配器

private class DownloadFile extends AsyncTask<String, Integer, String> { 
    @Override 
    protected String doInBackground(String... sUrl) { 
     try { 
      File mydownload = new File (Environment.getExternalStorageDirectory()+ "/Gazzetta Ufficiale"); 

      if (!mydownload.exists()){ 
       mydownload.mkdir(); 
      } 

      DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);  

      String url = sUrl[0]; 
      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
       request.allowScanningByMediaScanner(); 
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
      } 
       request.setAllowedNetworkTypes(
         DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE) 
         .setAllowedOverRoaming(false) 
         .setTitle("Gazzetta " + sUrl[1]) 
         .setDescription(sUrl [2] + ". In download..") 
         .setDestinationInExternalPublicDir("/Gazzetta Ufficiale", sUrl[1] + ".pdf"); 

      manager.enqueue(request); 


     } 
     catch (Exception e) { 
     } 
     return null; 
    } 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     toast = Toast.makeText(Gazzetta_detail.this, "Download in corso...", Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 
+0

您是否尝试在获取新数据后调用适配器上的notifyDataSetChanged()方法? –

+0

我在活动中获取新数据,如何将此metod命名为片段上的适配器?对不起,但是我的第一个应用程序! – bott91

+0

好吧我对碎片不是很熟悉,但我会尽力帮助你。我想你正在调用一个函数来更新你的活动列表视图,对吧?在这种情况下,您仍然可以在更新函数末尾调用我建议您使用的方法,如下所示:adapter.notifyDataSetChanged()。 –

回答

0

呼叫notifyDataSetChanged()

关于如何/何时调用notifyDataSetChanged()的一些额外细节可以在此 Google I/O video中查看。

+0

是的,我知道这个,但它不工作! – bott91

+0

显示你的整个代码.. – Riser

+0

现在我显示我的代码! – bott91