2016-04-01 19 views
0

私は、ユーザーが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); 

    } 

} 

}

答えて

関連する問題