简体   繁体   English

想要在新活动屏幕上显示存储的名称/值数据

[英]Want to display stored name/value data in intent to new activity screen

I am semi-ok with the understanding of how you store and retrieve data through intents, what I am not sure about however is the code implementation.我对如何通过意图存储和检索数据的理解还不错,但我不确定的是代码实现。 What I want to accomplish is to pass stored data (name/value pairs and a bundle object) to my ThirdActivity class from MainActivity class.我想要完成的是将存储的数据(名称/值对和包对象)从 MainActivity 类传递给我的 ThirdActivity 类。 Here is a snippet what i have attempted thus far (which when the user clicks on the a3button it just displays a blank page with no string):这是我迄今为止尝试过的一个片段(当用户点击 a3button 时,它只显示一个没有字符串的空白页面):

MainActivity from which I want to store data to send to ThirdActivity:我想从中存储要发送到 ThirdActivity 的数据的 MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button a2_button = (Button) findViewById(R.id.a2_button);
        a2_button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
               Intent a2intent = new Intent("com.tester.lab.x");
               startActivityForResult(a2intent, 1);
            }
        });

        Button a3_button = (Button) findViewById(R.id.a3_button);
        a3_button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent a3intent = new Intent(v.getContext(), ThirdActivity.class);

                a3intent.putExtra("greeting","my Name");
                startActivity(a3intent);

            }
        });
    }
}

ThirdActivity:第三个活动:

public class ThirdActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        getIntent().getStringExtra("greeting");

    }
}

How would I display the string "my Name" once the user clicks ThirdActivity button?一旦用户单击 ThirdActivity 按钮,我将如何显示字符串“我的名字”? Any help is wonderful, thanks!任何帮助都很棒,谢谢!

Everything is good, just add String type for value that you are getting and use this String as you want (to show in TextView else..):一切都很好,只需为您正在获取的值添加String类型,并根据需要使用此String (在TextView显示其他..):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_third);

    EditText text = (EditText) findViewById(R.id.your_textView_id);

    String value = getIntent().getStringExtra("greeting");
    text.setText(value);
}

Or by using Button :或者通过使用Button

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_third);

    EditText text = (EditText) findViewById(R.id.your_textView_id);
    Button three = (Button) findViewById(R.id.your_button_id);

    three.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           String value = getIntent().getStringExtra("greeting");
           text.setText(value);
        }
    });     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM