我想从两个活动(StudyProgram.java和LogIn.java)中设置对象(User.java)中的数据,并在运行时将其显示在单个活动(SecondActivity.java)或屏幕中,但我面对的是问题。我的代码在单个活动中显示共享数据,但不在同一时间或屏幕上显示。它分别在两个屏幕上显示。但是,我想在同一个屏幕上显示它,这对我来说很难。我的代码是
Login.java
Intent intent = new Intent(Login.this,SecondActivity.class);
user.setName(name);
user.setFacebookId(id);
intent.putExtra("User", user);
startActivity(intent);
StudyProgram.java
Intent intent = new Intent(getContext(),SecondActivity.class);
user.setVuId(data);
user.setStudyProgram(data1);
intent.putExtra("User", user);
startActivity(intent);
User.java
package com.technerdshub.vusocial.Models;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
/**
* Created by MFRajput on 01/05/16.
*/
public class User implements Serializable {
private String studyProgram;
private String name;
private String facebookId;
private String vuId;
public User(String studyProgram, String name, String facebookId, String vuId) {
this.studyProgram = studyProgram;
this.name = name;
this.facebookId = facebookId;
this.vuId = vuId;
}
public User() {
}
public String getVuId(String data) {
return vuId;
}
public void setVuId(String vuId) {
this.vuId = vuId;
}
public String getStudyProgram(String data1) {
return studyProgram;
}
public void setStudyProgram(String studyProgram) {
this.studyProgram = studyProgram;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFacebookId() {
return facebookId;
}
public void setFacebookId(String facebookId) {
this.facebookId = facebookId;
}
@Override
public String toString() {
return "User{" +
"studyProgram='" + studyProgram + '\'' +
", name='" + name + '\'' +
", facebookId='" + facebookId + '\'' +
", vuId='" + vuId + '\'' +
'}';
}
}
SecondActivity.java
package com.technerdshub.vusocial.Activities;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.technerdshub.vusocial.Models.User;
import com.technerdshub.vusocial.R;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
Intent intent = this.getIntent();
User user = (User) intent.getSerializableExtra("User");
TextView textView = (TextView) findViewById(R.id.textView2);
textView.setText(user.toString());
}
}
请有人帮助我,我该怎么办?
答案 0 :(得分:1)
如何使用单身人士?
public class User implements Serializable {
private static User instance;
private String studyProgram;
private String name;
private String facebookId;
private String vuId;
private User() {}
public static User getInstance(){
if (instance == null){
instance = new User();
}
return instance;
}
...
}
这是一个单例,没有必要将它传递给intent,因为它可以全局访问。
在Login.java中执行
Intent intent = new Intent(Login.this,SecondActivity.class);
User user = User.getInstance();
user.setName(name);
user.setFacebookId(id);
startActivity(intent);
在StudyPRogram.java中
Intent intent = new Intent(getContext(),SecondActivity.class);
User user = User.getInstance();
user.setVuId(data);
user.setStudyProgram(data1);
startActivity(intent);
在SecondActivity中执行:
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
User user = User.getInstance();
TextView textView = (TextView) findViewById(R.id.textView2);
textView.setText(user.toString());
}
答案 1 :(得分:0)
将所有用户变量和方法设为静态,然后通过User类访问它们,而不是创建用户类的实例。
User.setVuId(data);
而不是像
那样实例化它User user = new User();
user.setVuId(data);