如何在两个不同的活动之间传递多个字符串?

时间:2014-03-28 23:23:01

标签: java android json string parsing

我正在尝试将使用json解析的MainActivity类中的多个字符串传递给SingleContactActivity类。现在我正在测试只传递名称和电子邮件字符串,在logcat中它表示值已成功通过但我无法在SingleContactActivity中输出两个字符串。但是,如果我尝试传递名称字符串,它将输出到SingleContactActivity。 为了清晰代码,我删除了导入。

public class MainActivity extends ListActivity {

private ProgressDialog pDialog;

// URL to get contacts JSON
public static String url = "http://api.androidhive.info/contacts/";

// contacts JSONArray
JSONArray contacts = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    contactList = new ArrayList<HashMap<String, String>>();

    ListView lv = getListView();

    // Listview on item click listener
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name))
                    .getText().toString();
            String email = ((TextView) view.findViewById(R.id.email))
                    .getText().toString();

                            // Launching new Activity on selecting single List Item
            Intent in = new Intent(MainActivity.this, SingleContactActivity.class);

            // sending data to new activity
                in.putExtra("TAG_NAME", name);
            in.putExtra("TAG_EMAIL", email);

            startActivity(in);

        }
    });

    // Calling async task to get json
    new GetContacts().execute();
}

公共类SingleContactActivity扩展了Activity {

@Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       this.setContentView(R.layout.single_list_item_view);
            Bundle extras = getIntent().getExtras();
       if( extras != null ) {
           String name = extras.getString("TAG_NAME");
           Log.d("", "Value passed = " + name);
           TextView txtContact = (TextView) findViewById(R.id.name);
       txtContact.setText(name); 
               String email = extras.getString("TAG_EMAIL");
           Log.d("", "Value passed = " + email);
               TextView txtContact1 = (TextView) findViewById(R.id.email);
           txtContact1.setText(email); 
         }
     }
}

1 个答案:

答案 0 :(得分:0)

onCreate()方法中试用此代码:

Intent intent = getIntent();
   if( intent != null ) {
       String name = intent.getStringExtra("TAG_NAME");
       Log.d("", "Value passed = " + name);
       TextView txtContact = (TextView) findViewById(R.id.name);
       txtContact.setText(name); 
       String email = intent.getStringExtra("TAG_EMAIL");
       Log.d("", "Value passed = " + email);
       TextView txtContact1 = (TextView) findViewById(R.id.email);
       txtContact1.setText(email); 
     }