I have a AlertDialog which has a EditText. This EditText takes user input and on "Post" button click a API call is initiated.
The problem is the AlertDialog should dismiss as soon as the "Post" button is clicked. Currently I have to click on outside area to dismiss it.
If I remove the API call, the AlertDialog dismisses properly on button click.
I am not sure what is wrong.
Here is my code:-
case R.id.btnAddComms:
scrollNews.fullScroll(v.FOCUS_DOWN);
btnAddComms.setPressed(true);
AlertDialog.Builder builder = new AlertDialog.Builder(NDetails.this);
builder.setTitle("Post Comment");
builder.setIcon(R.drawable.post_comment_button);
final EditText input1 = new EditText(NDetails.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input1.setLayoutParams(lp);
builder.setView(input1);
builder.setPositiveButton("Post", new DialogInterface.OnClickListener()
{
//private ITextInputDialogCalback callback;
public void onClick(DialogInterface dialog, int id)
{
postedComment = input1.getText().toString();
if(postedComment.length()>0)
{
dialog.cancel();
PostComments(postedComment);
}
else
{
Toast.makeText(NewsDetails.this, "Please enter a comment.", Toast.LENGTH_LONG).show();
input1.findFocus();
}
}
private void PostComments(String postedComment)
{
// TODO Auto-generated method stub
String postCommentUrl = url;
try
{
String commentResponse = new PostComment().execute(postCommentUrl).get();
String getRequestForComments = myurl;
String items = new FetchItems().execute(getRequestForComments).get();
ArrayList<HashMap<String, String>> updatedCommentList = new GetList().execute(items).get();
itemsAdapter = (ListAdapter) new CommentsAdapter(NDetails.this, updatedCommentList);
commentsList.invalidate();
commentsList.refreshDrawableState();
commentsList.setAdapter(itemsAdapter);
commentsList.post(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
commentsList.setSelection(itemsAdapter.getCount()-1);
}
});
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ExecutionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
})
.setCancelable(false);
AlertDialog alert = builder.create();
alert.setCanceledOnTouchOutside(true);
alert.show();
break;