如何从hashmap获取值并显示测验应用..?

时间:2013-01-30 13:01:37

标签: android textview radio-button hashmap

我在测验应用中遇到问题......

我的问题是......

我通过JSON得到了问题,问题和答案。我用单键存储在hashmap中。现在我想做的是......

如何根据问题得到hashmap的问题和答案..在textview中显示问题并在radiobuttons中显示答案......

我该如何实现?任何帮助将不胜感激...非常感谢提前..

  protected void onPostExecute(String file_url) {
   pDialog.dismiss();   
       List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tid", tid));
    JSONObject json = jsonParser.makeHttpRequest(url_get_quesid, "GET", params);
    json = jsonParser.makeHttpRequest(url_get_quesurl, "GET", params);
      try {
    int success = json.getInt(TAG_SUCCESS);
    if (success == 1) {
    System.out.println("Success");
    groups = json.getJSONArray(TAG_GROUP);
    System.out.println("Result Success+++"+groups);
    for (int i = 0; i < groups.length();i++) {
    JSONObject c = groups.getJSONObject(i);
    String questionid = c.getString(TAG_QUESID);
    String question = c.getString(TAG_QUES);
    String answer = c.getString(TAG_ANSW);
    System.out.println("Checking ::"+questionid);
    id=questionid;         
    quesid.add(questionid);
    HashMap<String, String> map = new HashMap<String, String>();
    map.put(questionid,  question + ";" + answer);                  
    System.out.println("QuestionIDMap:"+map);
        }
    } else {
    showAlert();
}
    } catch (JSONException e) {
    System.out.println("Error "+e.toString());
    }       
}

2 个答案:

答案 0 :(得分:1)

首先,您要将questionid用作地图键。

一旦你有了,你可以简单地做一些类似的事情;

String total = map.get(questionid); String [] split = total.split(“;”);

String question = split [0]; String answer = split [1];


总的来说,我宁愿建议使用数据库,然后将所有内容保存在集合中。

答案 1 :(得分:0)

maponCreate()的本地人。onCreate()。这意味着当Activity完成后,您的地图将被销毁,您将无法再访问它。将地图移动为map.get(questionId)的字段。然后,您可以从任意位置访问它,并public class YourActivity extends Activity { private HashMap<String, String> questions = new HashMap<String, String>(); protected void onPostExecute(String file_url) { pDialog.dismiss(); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("tid", tid)); JSONObject json = jsonParser.makeHttpRequest(url_get_quesid, "GET", params); json = jsonParser.makeHttpRequest(url_get_quesurl, "GET", params); try { int success = json.getInt(TAG_SUCCESS); if (success == 1) { System.out.println("Success"); groups = json.getJSONArray(TAG_GROUP); System.out.println("Result Success+++"+groups); for (int i = 0; i < groups.length();i++) { JSONObject c = groups.getJSONObject(i); String questionid = c.getString(TAG_QUESID); String question = c.getString(TAG_QUES); String answer = c.getString(TAG_ANSW); System.out.println("Checking ::"+questionid); id=questionid; quesid.add(questionid); questions.put(questionid, question + ";" + answer); System.out.println("QuestionIDMap:"+map); } } else { showAlert(); } } catch (JSONException e) { System.out.println("Error "+e.toString()); } } } 来获取问题。

您的活动将如下所示:

Activity

然后,您可以通过questions.get(questionId)

在{{1}}的任意位置访问该问题