Alert对话框中的TextView

时间:2016-04-01 16:18:23

标签: java android textview alertdialog

我的应用程序中有一个问题功能,用户将问题输入到editext行,API会检索答案。当用户点击提交时,我想要一个警告对话框来显示答案。截至目前,他们被重定向到另一个屏幕,只显示答案文本。我想将答案显示在警告对话框中的textview中。有人可以帮忙吗。以下是我的问题类代码。

Question.java

public class Question extends ActionBarActivity {

String api = "https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/quickAnswer?";
private String question;
private String answer;
private ProgressDialog pDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String unFilteredQuestion = extras.getString("question");
        question = unFilteredQuestion.replaceAll("\\s+", "%20");
        System.out.println(question);
        new query().execute();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_question, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private class query extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(Question.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        JSONObject jsonStr = null;
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httppost = new HttpGet(api+"q=" + question);
        httppost.setHeader("X-Mashape-Authorization", "5VSMYMsFj4msh0QQAjh7CCxfTaQqp1WVtbmjsnGgPs5B2mmY5k");

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            String responseBody = httpclient.execute(httppost, responseHandler);
            jsonStr = new JSONObject(responseBody);

        }catch (Exception e){e.printStackTrace();}

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {

                answer = jsonStr.getString("answer");

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();

        TextView text = (TextView)findViewById(R.id.answer);
        text.setText(answer);

    }

}

}

1 个答案:

答案 0 :(得分:0)

new AlertDialog.Builder(context)
    .setMessage(answer)
    .show();

http://developer.android.com/reference/android/app/AlertDialog.Builder.html