0

I have a problem with a ListView on android. Is not really a ListView, because it's implemented on a Sherlock Fragment. So in particular: I have 2 classes, one is the Fragment, and one is a normal Activity. The Activity downloads one file from the web, and in the Fragment there is a list with the downloaded files.
This is my adapter for the 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);

So the adapter catches all the files in the Sample Folder and put it on the list. When I download a file, the adapter doesn't refresh the list (I think because it is in a different context, but I don't know the solution for that). To refresh I have to close and open the application. I tried a lot of possible solution but nothing helped.

edit: my Fragment code:

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

Download activity:

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

1 回答 1

0

致电notifyDataSetChanged()您的适配器。

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

于 2013-06-04T10:01:11.260 回答