自定义ListView不刷新

时间:2013-06-04 09:59:24

标签: android listview refresh android-fragmentactivity

我在Android上遇到ListView的问题。实际上不是ListView,因为它是在Sherlock Fragment上实现的。特别是: 我有2个班,一个是Fragment,一个是普通ActivityActivity从网络下载一个文件,在Fragment中有一个包含下载文件的列表。
这是Fragment的适配器:

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

1 个答案:

答案 0 :(得分:0)

在适配器上拨打notifyDataSetChanged()

可以在此查看有关如何/何时调用notifyDataSetChanged()的一些其他细节 Google I/O video