对ListView只使用一个AlertDialog

时间:2014-03-14 15:54:08

标签: java android alertdialog android-alertdialog

我有一个包含5个项目的列表视图。如果我点击一个项目,它会启动一个AlertDialog,询问我是否要下载该文件。每个项目,都有不同的网址下载。 而不是创建5 AlertDialog,我可以创建一个AlertDialog并在所选项目上启动正确的下载URL吗?

public class MapsListActivity extends Downloader implements OnItemClickListener{
private static final File MAP4 = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/", "map4.map");
private static final File MAP3 = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/", "map3.map");
private static final File MAP2 = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/", "map2.map");
private static final File MAP = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/", "map.map");





ListView listView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String osmdroidFolder = "/osmdroid/";
      String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
      File myOsmdroidFolder = new File(extStorageDirectory + osmdroidFolder);
      myOsmdroidFolder.mkdir();

  String tilesFolder = "/osmdroid/tiles/";
      File myTilesFolder = new File(extStorageDirectory + tilesFolder);
      myTilesFolder.mkdir();

    listView = (ListView) findViewById(R.id.mapsList);
    listView.setOnItemClickListener(this);

}






@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
    switch (position){
    case 0:{
        Intent Info = new Intent(MapsListActivity.this, MyMapsActivity.class);
        startActivity(Info);
    }
    break;
    case 1:{

        if (MAP.exists()) {
            Intent Info = new Intent(MapsListActivity.this, MyMapsActivity.class);
            startActivity(Info);
        }
        else{
            DialogDownload();
        }   
    }
    break;
    case 2:{

        if (MAP2.exists()) {
            Intent Info = new Intent(MapsListActivity.this, MyMapsActivity.class);
            startActivity(Info);
        }
        else{
            DialogDownload();
        }   
    }
    break;
    case 3:{

        if (MAP3.exists()) {
            Intent Info = new Intent(MapsListActivity.this, MyMapsActivity.class);
            startActivity(Info);
        }
        else{
            DialogDownload();
        }   
    }
    break;

    case 4:{

        if (MAP4.exists()) {
            Intent Info = new Intent(MapsListActivity.this, MyMapsActivity.class);
            startActivity(Info);
        }
        else{
            DialogDownload();
        }   
    }
    break;

    }

}


protected void DialogDownload() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Maps not present. Would you like to download ?");
    builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

       @Override
       public void onClick(DialogInterface dialog, int item) {

           /* functions that start the download

           downloadmap();
           downloadmap2();
           downloadmap3();
           downloadmap4();

           */


            dialog.dismiss();

       }
    });

    builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

       @Override
       public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
       }

    });


    AlertDialog alert = builder.create();
    alert.show();
    }

 }

1 个答案:

答案 0 :(得分:1)

public void DialogDownLoad(String message,int pos)

{ 

      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

      // set title
      alertDialogBuilder.setTitle("title");

      // set dialog message
      alertDialogBuilder.setMessage(message)
                  .setCancelable(false)
                  .setPositiveButton("Yes",new  DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        if(pos == 0){
                           // call downloadMap1();
                        }
                        if(pos == 1){
                          // call  downloadMap2();
                        }
                        ......................
                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        ...............   //your code
                    }
                });

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}

并在你的onItemClick()方法中

@Override
public void onItemClick(AdapterView<?> av, View v, int pos, long id) {

   message = "Your url";      
   DialogDownload(message,pos);
}