简体   繁体   中英

TextView in Alert dialog

I have a question functionality in my application in which the user inputs a question into an editext line and the API retrieves the answer. When the user hits submit I would like an alert dialog to show with the answer. As of now they are re directed to another screen with just the answer text displayed. I would like the answer to be displayed in a textview inside an alert dialog instead. Can some one help please. The following is my code for the question class.

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);

    }

}

}

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

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

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