2013-06-04 3 views
0

android에 ListView에 문제가 있습니다. 실제로는 ListView이 아닙니다. Sherlock Fragment에 구현 되었기 때문입니다. 그래서 특히 : 나는 2 개의 클래스를 가지고 있는데, 하나는 Fragment이고, 하나는 보통 Activity입니다. Activity은 웹에서 하나의 파일을 다운로드하고 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 내 어댑터입니다. 파일을 다운로드 할 때 어댑터가 목록을 새로 고치지 않습니다 (다른 컨텍스트에 있기 때문에 생각하지만 해결책은 모르겠습니다). 새로 고침하려면 응용 프로그램을 닫고 열어야합니다. 나는 가능한 많은 해결책을 시도했지만 아무 도움도받지 못했다.

편집 : 내 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().

Google I/O video에서 notifyDataSetChanged()를 호출하는 방법과시기에 대한 몇 가지 추가 세부 정보를 볼 수 있습니다.

+0

그래, 이건 알아,하지만 그다지 효과가 없어! – bott91

+0

전체 코드를 표시하십시오 .. – Riser

+0

이제 코드를 보여 드리겠습니다! – bott91

관련 문제