Вопрос

How to show Alert Dialog in static method, i am trying to put a condition, in which i am checking for the folder inside the SD Card, if exist then listing Items, otherwise i want to show AlertDialog - with message no folder found with Church Name

  public static List <String> fromSDCard()
    {
        List <String> listChurchWall = new ArrayList <String>();
        // listing Wallpaper using church names
        String string = "/mnt/sdcard/Church/Wallpaper/";
        f = new File (string+name+"/");
        if (f.exists()) 
         {
            files = f.listFiles ();
         }else{
        // here i want to put AlertDialog       
         }                          
    return listChurchWall;  
    }   
Это было полезно?

Решение

Pass your app context to the static method.

public static List <String> fromSDCard(Context context)
{
    List <String> listChurchWall = new ArrayList <String>();
    // listing Wallpaper using church names
    String string = "/mnt/sdcard/Church/Wallpaper/";
    f = new File (string+name+"/");
    if (f.exists()) 
     {
        files = f.listFiles ();
     }else{
         // 1. Instantiate an AlertDialog.Builder with its constructor
         AlertDialog.Builder builder = new AlertDialog.Builder(context);

         // 2. Chain together various setter methods to set the dialog characteristics
         builder.setMessage(R.string.dialog_message)
         .setTitle(R.string.dialog_title);

         // 3. Get the AlertDialog from create()
        AlertDialog dialog = builder.create();

        // 4. Show the dialog
        dialog.show()
     }                          
     return listChurchWall;  
}   

If calling from your activity.

public MyActivity extends Activity
{
    ....

    private void Method()
    {
         List<String> list = fromSdCard(this);
    }

    ....
}

Другие советы

public static List<String> fromSDCard(Context mContext) {
        List<String> listChurchWall = new ArrayList<String>();
        // listing Wallpaper using church names
        String string = "/mnt/sdcard/Church/Wallpaper/";
        f = new File(string + name + "/");
        if (f.exists()) {
            files = f.listFiles();
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

            int imageResource = android.R.drawable.stat_sys_warning;
            Drawable image = mContext.getResources().getDrawable(imageResource);

            builder.setTitle("title").setMessage("your Message").setIcon(image).setCancelable(false).setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            });

            AlertDialog alert = builder.create();
            alert.setCancelable(false);
            alert.show();
        }
        return listChurchWall;
    }

Try the following way--

public static List <String> fromSDCard(Activity a, String title, String message)
 {
    List <String> listChurchWall = new ArrayList <String>();
    // listing Wallpaper using church names
    String string = "/mnt/sdcard/Church/Wallpaper/";
    f = new File (string+name+"/");
    if (f.exists()) 
    {
      files = f.listFiles ();
    }
    else
    {
      AlertDialog.Builder dialog = new AlertDialog.Builder(a);
      dialog.setTitle(title);
      dialog.setMessage(message);
      dialog.setNeutralButton("OK", null);
      dialog.create().show();      
    }                          
    return listChurchWall;  
 }   

Then in your class do---

public MyActivity extends Activity
{
    ....

    private Method()
    {
         List<String> list = fromSdCard(this, "Your Title", "Your message");
    }

    ....
}

UPDATE:

You get a NullPointerException because something is null that shouldn't be. It happens while sorting the array, so perhaps one of the array elements is null. Take a look at how you assign values to your array.

Perhaps at the top of it, see if any of the objects Object o1 or Object o2 themselves are null.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top