[英]passing string values from one activity to another in android development
hi im trying to pass string values that a user enters from one activity to another and have those values displayed in a table on the second activity, however when we move to the second activity the table is displayed but its empty, i was wondering if anyone could help me find where my problem is in my code, 您好,我试图将用户从一个活动输入的字符串值传递给另一个活动,并在第二个活动的表中显示这些值,但是当我们移至第二个活动时,该表显示但其为空,我想知道是否有人可以帮助我找到代码中的问题所在,
<h1>Activity 1 where user enters information</h1>
`package com.example.scheduler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Save extends Activity implements OnClickListener
{
Button sbm,bk;
EditText fname,lname,course,time,inst,title;
private static final int table = 1810;
private String newfname,newlname,newcourse,newtitle,newtime,newinst;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newb);
sbm = (Button) findViewById(R.id.button1);
sbm.setOnClickListener(this);
bk = (Button) findViewById(R.id.button2);
bk.setOnClickListener(this);
fname = (EditText) findViewById(R.id.editText1);
lname = (EditText) findViewById(R.id.editText2);
course = (EditText) findViewById(R.id.editText3);
time = (EditText) findViewById(R.id.editText4);
inst = (EditText) findViewById(R.id.editText5);
title = (EditText) findViewById(R.id.editText6);
}
public void onClick(View v)
{
v.getId();
try
{
Intent launch = new Intent(Save.this,table.class);
Bundle myData = new Bundle();
if(sbm.isPressed())
{
if(fname.getText().toString().length()==0||lname.getText().toString().length()==0||course.getText().toString().length()==0||time.getText().toString().length()==0
||inst.getText().toString().length()==0||title.getText().toString().length()==0)
{
Toast.makeText(this, "Please fill in all fields!", Toast.LENGTH_LONG).show();
}else
{
newfname = fname.getText().toString();
newlname = lname.getText().toString();
newcourse = course.getText().toString();
newtime = time.getText().toString();
newinst = inst.getText().toString();
newtitle = title.getText().toString();
myData.putString("fname", newfname);
myData.putString("lname", newlname);
myData.putString("course", newcourse);
myData.putString("time", newtime);
myData.putString("instructor", newinst);
myData.putString("title", newtitle);
startActivityForResult(launch,table);
}
}
}catch(Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
}
if(bk.isPressed())
{
finish();
}
}
}
`
<h1>Activity 2 where information is displayed</h1>
package com.example.scheduler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class table extends Activity
{
TextView titletv,fnametv,lnametv,coursetv,timetv,insttv;
public void onCreate(Bundle savedInstance)
{
super.onCreate(savedInstance);
setContentView(R.layout.schedule_table);
titletv = (TextView) findViewById(R.id.titleTextView);
fnametv = (TextView) findViewById(R.id.fnameTextView);
lnametv = (TextView) findViewById(R.id.lnameTextView);
coursetv = (TextView) findViewById(R.id.courseTextView);
timetv = (TextView) findViewById(R.id.timeTextView);
insttv = (TextView) findViewById(R.id.instructorTextView);
try
{
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
String strfname = myBundle.getString("fname");
String strlname = myBundle.getString("lname");
String strcourse = myBundle.getString("course");
String strtime = myBundle.getString("time");
String strinst = myBundle.getString("instructor");
String strtitle = myBundle.getString("title");
titletv.setText(strtitle);
fnametv.setText(strfname);
lnametv.setText(strlname);
coursetv.setText(strcourse);
timetv.setText(strtime);
insttv.setText(strinst);
myLocalIntent.putExtras(myBundle);
setResult(Activity.RESULT_OK, myLocalIntent);
}catch(Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
}
finally
{
}
}
}`
您必须将bundle
放入Intent
launch.putExtras(myData)
You declare: 您声明:
Bundle myData = new Bundle();
But you are starting activity with table: 但是您正在使用表开始活动:
startActivityForResult(launch,table);
Change it to: 更改为:
launch.putExtras(myData)
you can declaer the string in the main activity like this 您可以像这样在主要活动中对字符串进行修饰
static String yourString="";
assign the value to your string 将值分配给您的字符串
yourString="what_Ever_Value";
and call it from the other activity like the following: 并从其他活动中调用它,如下所示:
String yourString=packgeName.ClassName.yourString;
If this is Still open and being looked at you really should be using shared preferences for this. 如果仍然打开并正在查看,那么您确实应该为此使用共享的首选项。 http://developer.android.com/guide/topics/data/data-storage.html#pref
http://developer.android.com/guide/topics/data/data-storage.html#pref
simpler and more efficient than intents and with shared prefs you can pass data to fragments as well as activities and vice versa 比意图更简单,更有效,并且可以使用共享首选项将数据传递给片段以及活动,反之亦然
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.