Uygulamamda, kullanıcının bir sorgusuz satırına soru yazdığı ve API'nin yanıtı aldığı bir soru işlevselliği var. Kullanıcı gönderdiğinde, cevabı göstermek için bir uyarı iletişim kutusu istiyorum. Şu an itibariyle sadece görüntülenen cevap metni ile başka bir ekrana yönlendiriliyorlar. Cevabın yerine bir uyarı diyalogu içinde bir metin görünümünde görüntülenmesini istiyorum. Biri lütfen yardım edebilir. Aşağıdaki soru sınıfı için kodum.Alert iletişim kutusunda 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);
}
}
}