如何从一个活动发送信息,在第二个活动中接收信息并将其发送到主要活动?

时间:2014-03-31 09:00:55

标签: android

我想从一个页面发送信息并在编辑页面中接收它,并在收到它之后我想保存它并将其发送到主要活动(在android中)。 我该怎么做? 这是第一页的代码:

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            int position1 = position;
            // i am getting the title of the movie that was pressed
            String search_title = list.get(position).getTitle().toString();
            String search_url = list.get(position).getPhoto_url();
            String search_description = list.get(position).getDescription();

            Intent intent = new Intent(Search_A_Movie.this,Edit_A_Movie.class);

            // i am sending the info of the movie to the edit_a_movie page
            intent.putExtra("item_title", search_title);
            intent.putExtra("item_url", search_url);
            intent.putExtra("item_description", search_description);

            startActivity(intent);

        }
    });

4 个答案:

答案 0 :(得分:1)

使用SharedPreference。保存在A1中并在A2中检索,反之亦然。

<强>初始化

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();

存储数据

editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long

editor.commit(); // commit changes

检索数据

// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean

删除数据

editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes

清算存储空间

editor.clear();
editor.commit(); // commit changes

答案 1 :(得分:1)

您只需发送一项活动的数据

像这样发送......

Intent intent = new Intent(Search_A_Movie.this,Edit_A_Movie.class);

            // i am sending the info of the movie to the edit_a_movie page
            intent.putExtra("item_title", search_title);
            intent.putExtra("item_url", search_url);
            intent.putExtra("item_description", search_description);

            startActivity(intent);

获取第二个活动中的值,就像这样......

Intent intent = getIntent();
        String name1 = intent.getStringExtra(SENTNAME1);
        String name2 = intent.getStringExtra(SENTNAME2);//replace the above sent name what you have given in the first activity....
        String name3 = intent.getStringExtra(SENTNAME3);

答案 2 :(得分:0)

试试这个:

SharedPreferences sharedPreferences = PreferenceManager
        .getDefaultSharedPreferences(getApplicationContext());

SharedPreferences.Editor editor = sharedPreferences.edit(); 

editor.putString("PASSWORD_KEY", mPassword);
editor.commit();

从另一个活动获取已保存的字符串值

SharedPreferences sharedPreferences = PreferenceManager
        .getDefaultSharedPreferences(getApplicationContext());
String s = sharedPreferences.getString("PASSWORD_KEY", "");  

答案 3 :(得分:0)

在第二项活动中

  Bundle d = getIntent().getExtras();
    String item_title = d.getString("item_title");
    String item_url = d.getString("item_url");
    String item_description = d.getString("item_description");