简体   繁体   中英

Simple Alert Dialog Issue - Android

I have an app I am working on that uploads a picture to a facebook album. The app uploads the photo fine, no issues there. I can also put a caption on the photo if I hard code it. What I am trying to do is make a alert dialog that will capture the users caption and then place that in the bundle before uploading the picture. What is happening is the photo is uploaded then after that I get the dialog box to enter the caption.

Here is the method to pop the alert dialog...

public String createAlert() {      
        AlertDialog.Builder alert = new AlertDialog.Builder(this);                 
          alert.setTitle("Enter Caption for Photo");  
          alert.setMessage("Caption :");
          final EditText input = new EditText(this); 
          alert.setView(input);

          alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int whichButton) {  
                    imageCaption = input.getText().toString();
                    return;                  
                   }  
                 });  

                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        return;   
                    }
                });
               AlertDialog helpDialog = alert.create();
               helpDialog.show();
               return imageCaption;

  }

Now here is the bundle and upload to facebook...

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);        
        switch (requestCode) {
        case PICK_EXISTING_PHOTO_RESULT_CODE: {   

        if (resultCode == RESULT_OK){
              Uri photoUri = data.getData();
              String imagePath = getPath(photoUri);
              byte[] data1 = null;


                Bitmap bi = BitmapFactory.decodeFile(imagePath);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                data1 = baos.toByteArray();

                Bundle params = new Bundle();
                params.putString(Facebook.TOKEN, facebook.getAccessToken());
                params.putString("caption", createAlert() );
                params.putByteArray("photo", data1);

                try {
                    facebook.request("me/photos",params,"POST");
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
              }


        break;
        }
        default: {
        facebook.authorizeCallback(requestCode, resultCode, data);
        break;
        }


    }

    }

At the time your createAlert method finishes, the user has not entered anything. An event to show the dialog has just been added to the system messages that will be processed in the future once your code stops running. Your code then goes on to make the facebook post. Then the dialog is shown. Then the code in the OnClickListener gets run when the right thing is clicked.

You need to send the Facebook post after the dialog action happens. I suppose the straight forward way to do this would be to inline the createAlert method right above the line where you call it now. Then stick the rest of the Facebook code in the onClick. That would then give you the proper order and you could break things up into methods again.

Just realize that calling show doesn't do anything immediately and that code in an onclick isn't run immediately either. This is just queuing up an event and specifying what happens when another event occurs, respectively. It's event based programming, like many GUI APIs.

Edit: I'll try to edit your code to show how to make it work. You didn't post enough for it to compile, however, so there may be some cleanup to do. Here:

protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case PICK_EXISTING_PHOTO_RESULT_CODE:

            if ( RESULT_OK == resultCode) {
                final Uri photoUri = data.getData();
                createAlert(photoUri);
            }

            break;
        default:
            facebook.authorizeCallback(requestCode, resultCode, data);
            break;
    }
}

public void createAlert(final Uri photoUri) {
    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Enter Caption for Photo");
    alert.setMessage("Caption :");

    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(final DialogInterface dialog, final int whichButton) {
            final String caption = input.getText().toString();
            postToFacebook(photoUri, caption);
        }
    });

    alert.setNegativeButton("Cancel", null);
    final AlertDialog helpDialog = alert.create();
    helpDialog.show();
}

private void postToFacebook(final Uri photoUri, final String caption) {
    final String imagePath = getPath(photoUri);
    final byte[] data1 = null;

    Bitmap bi = BitmapFactory.decodeFile(imagePath);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    data1 = baos.toByteArray();
    bi.recycle();
    bi = null;

    final Bundle params = new Bundle();
    params.putString(Facebook.TOKEN, facebook.getAccessToken());
    params.putString("caption", caption);
    params.putByteArray("photo", data1);

    try {
        facebook.request("me/photos", params, "POST");
    } catch (final FileNotFoundException e) {
        e.printStackTrace();
    } catch (final MalformedURLException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM