如何将ArrayList发送到另一个Activity并在那里显示? 我想从另一个活动中获取数据并将其发送到另一个活动并打印出来。我只能传递单个字符串但不能传递字符串数组
Java文件代码,
主要活动:
EditText et1 , et2 , et3 , et4 ;
public final static String MESSAGE_KEY = "com.example.prabhav.myapplication.message";
ArrayList<String> ar , tr = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_on);
}
public void sm (View v)
{
et1= (EditText) findViewById(R.id.name);
String msg1 = et1.getText().toString();
et2= (EditText) findViewById(R.id.dob);
String msg2 = et2.getText().toString();
et3= (EditText) findViewById(R.id.emailinput);
String msg3 = et3.getText().toString();
et4= (EditText) findViewById(R.id.clgi);
String msg4 = et4.getText().toString();
ar.add(msg1);
ar.add(msg2);
ar.add(msg3);
ar.add(msg4);
tr=ar;
Intent intent = new Intent(this,SecAct.class);
intent.putExtra("myarray",tr);
startActivity(intent);
}
另一项活动:
Spinner s;
public final static String MESSAGE_KEY = "com.example.prabhav.myapplication.message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
ArrayList<String> ls = (ArrayList<String>) getIntent().getSerializableExtra("myarray");
ArrayAdapter<String> adptr=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ls);
s= (Spinner) findViewById(R.id.sp);
s.setAdapter(adptr);
// setContentView(R.layout.sec_lay);
}
答案 0 :(得分:3)
尝试如下
MainActivity
Intent i = new Intent(this,SecAct.class);
i.putStringArrayListExtra("myarray", tr);
startActivity(i);
第二项活动
ArrayList<String> list=(ArrayList<String>)getIntent().getStringArrayListExtra("myarray");
//use the list as you wish
希望这会对你有所帮助。
答案 1 :(得分:0)
尝试使用 putParcelableArrayList 发送arrayList,如下所示:
Intent intent = new Intent(this,SecAct.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("myArray", (ArrayList<? extends android.os.Parcelable>) tr);
intent.putExtras(bundle);
startActivity(intent);
你可以在第二个活动中得到arrayList:
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
tr= bundle.getParcelableArrayList("myArray");}
我希望这会有所帮助