【问题标题】:How to pass string from one activity to another [duplicate]如何将字符串从一个活动传递到另一个活动[重复]
【发布时间】:2016-03-18 14:37:51
【问题描述】:

我想将字符串从一个活动传递到另一个活动。在第二个活动中,字符串应该是可读的。

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
First actvity code
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long id) {

            // TODO Auto-generated method stub

            Intent dash_des = new Intent(getApplicationContext(),Dashboard_Description__page.class);

           //here should be t my string


        }
    });
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard__description__page);

        //here i want to read the string data
        }

【问题讨论】:

标签: java android


【解决方案1】:

您可能应该阅读有关 Android 中的 Intents 的信息。

您可以通过以下方式实现:

Intent myIntent = new Intent(FirstActivity.this,SecondActivity.class);
myIntent.putExtra("TestString","TestValue");
startActivity(myIntent);

以上代码将启动 SecondActivity。现在在第二个活动的 onCreate() 中,您需要:

String stringCameFromFirstAcvitity = getIntent().getStringExtra("TestString");

stringCameFromFirstAcvitity 的值将是“TestValue”

你的情况

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//First actvity code
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long id) {

        // TODO Auto-generated method stub

    Intent dash_des = new Intent(getApplicationContext(),Dashboard_Description__page.class);
    Object obj = listview.getAdapter().getItem(position);
    String value = obj.toString();
    dash_des .putExtra("TestString",value);
    startActivity(dash_des );
    }
});

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard__description__page);

    //here i want to read the string data
    }

secondActivity 的代码保持不变。希望对你有帮助

【讨论】:

  • 我需要 TestValue 作为我点击的动态值
  • 我正在相应地编辑我的答案
  • 我对编码很陌生..我想获得bookmark_title的值..而不是poision。
  • 是的,你是对的,它不是占据位置,它是占据位置的项目并获取该项目的名称并写入它。那不工作吗?您得到什么输出以及您期望什么,请在此处发布。
  • 我得到这个输出 com.example.compy.project_app.Actors@b4e65fa8
【解决方案2】:
Intent dash_des = new Intent(getApplicationContext(),Dashboard_Description__page.class);

dash_des.putString("your_key", your_string_variable);
startActivity(dash_des);

在第二个活动中:

Bundle bundle = getIntent().getExtras();
String text = bundle.getString("your_key");

【讨论】:

    【解决方案3】:

    当您需要将多个字符串从一个活动传递到另一个活动时,将它们捆绑并将其传递给下一个活动始终是最佳实践。 像这样的:

    Intent in = new Intent(Firstactivity.this, Secondactivity.class);
        Bundle mbundle = new Bundle();
        mbundle.putString("name", name);
        mbundle.putString("phonenumber", phonenumber);
        in.putExtras(mbundle);
    

    它们同样可以从第二个活动中检索到,如下所示,

     Bundle bundle = getIntent().getExtras();
     String name= bundle.getString("name");
     String phoneNumber= bundle.getString("phonenumber");
    

    【讨论】:

    • 如何传递电话号码
    • 您可以通过意图传递任何数据类型。假设您的电话号码是整数格式,您可以执行 mbundle.putInt("phonenumber",phonenumber);
    【解决方案4】:

    在意图中将 extring 作为额外的..

    然后在activity2中按住它

    示例:

    Intent intent = new Intent(current.this, Activity2.class);
    intent.putExtra("key","value");
    startActivity(intent);
    

    在Activity2中:

    String data = getIntent().getExtras().getString("key");
    

    【讨论】:

    • 字符串数据 = getIntent().getExtras().getString("key"); System.out.println(数据);这给了我错误
    • 你得到什么错误?
    猜你喜欢
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多