我创建Profile类,例如
@Entity
public class Profile {
@ColumnInfo(name = "name")
private String Name;
@ColumnInfo(name = "location")
private String location;
@ColumnInfo(name = "type")
private String type;
GET SET value set hear.
@Override
public String toString() {
return "\n{" +
"pid=" + pid +
", Name='" + Name + '\'' +
", Location='" + location + '\'' +
", Type='" + type + '\'' +
'}' + "\n";
}
}
然后在我的Activity类中,我想通过Intent将这些数据共享到另一个Activity解析中。怎么可能?
IN活动课
private List<Profile> profileList = new ArrayList<>();
private ArrayList<String> data = new ArrayList<>();
Profile profile = new Profile();
profile.setPid(new Random().nextInt());
profile.setName(mName);
profile.setLocation(mLocation);
profile.setType(mType);
data.all(profileList);
我的DAO课
@Dao
public interface ProfileDao {
@Query("SELECT * FROM profile")
List<Profile> getAll();
@Query("SELECT * FROM profile WHERE pid IN (:userIds)")
List<Profile> loadAllByIds(int[] userIds);
@Insert
void insertAll(Profile... profiles);
@Delete
void delete(Profile profile);
@Update
void update(Profile... profiles);
}
然后将profileList的相同数据保存在String中,然后将一个Activity发送到另一个Activity。 预先感谢..!
答案 0 :(得分:0)
使用putParcelableArrayListExtra
代替putExtra
Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
mIntent.putParcelableArrayListExtra("Data", mArraylist);
startActivity(mIntent);
OR
尝试如下使用gson
Gson gson = new Gson();
String jsonprofileList = gson.toJson(profileList );
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("profile_list ", jsonprofileList );
startActivity(intent);
并像
一样接收String profileListAsString = getIntent().getStringExtra("profile_list");
Gson gson = new Gson();
Type type = new TypeToken<List<modelClass>>(){}.getType();
List<modelClass> profileList = gson.fromJson(profileListAsString , type);