如何在同一活动中投入并获得价值?

时间:2013-02-08 07:37:49

标签: android android-intent

我想在相同的活动中放置并获得价值..我们能做到这一点..有可能吗?

我这样写的可能是同样的活动...

    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 question = c.getString(TAG_QUES);
        System.out.println("Checking ::"+question);
        ques1.add(question);
        String questionid = c.getString(TAG_QUESID);
        System.out.println("Checking ::"+questionid);
        id=questionid;
        quesid.add(questionid);
         }
    } else {
        showAlert();
    }
} catch (JSONException e) {
    System.out.println("Error "+e.toString());
}
    List<NameValuePair> params1 = new ArrayList<NameValuePair>();
    params1.add(new BasicNameValuePair("questionid", questionid)); // here i want to pass the questionid
    json = jsonParser.makeHttpRequest(url_get_ansurl, "GET", params1);
    System.out.println("ques value got");
    Log.d("All Groups: ", json.toString());
    System.out.println("question");
    try {
    int success = json.getInt(TAG_SUCCESS);
    System.out.println("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 answer = c.getString(TAG_ANSW);
            System.out.println("Checking ::"+answer);
            answ1.add(answer);
         }
    } else {
        showAlert();
    }
} catch (JSONException e) {
    System.out.println("Error "+e.toString());
}
protected void onPostExecute(String file_url) {
    pDialog.dismiss();
 ques1=new ArrayList<String>(new ArrayList<String>(ques1));
    //  j=0;
    TextView txtque = (TextView) findViewById(R.id.que_txt); 
    txtque.setText(ques1.get(j));

2 个答案:

答案 0 :(得分:0)

使用此...

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String keyIdentifer  = null;
i.putExtra("STRING_I_NEED", strName);
Then, to retrieve the value try something like:

String newString;
if (savedInstanceState == null) {
    extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}

答案 1 :(得分:0)

我猜共享偏好会解决这个问题

    SharedPreferences.Editor editor = app_preferences.edit();
    editor.putInt("counter", counter);
    editor.commit();


    SharedPreferences app_preferences = 
        PreferenceManager.getDefaultSharedPreferences(this);
    int counter = app_preferences.getInt("counter", 0);