代码之家  ›  专栏  ›  技术社区  ›  Viral Patel

无法从转换表达式数据的类型java.util.ArrayList<java.lang.String文件>至java.util.ArrayList<com.demo.Profile简介>

  •  -1
  • Viral Patel  · 技术社区  · 7 年前

    @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解析中。怎么可能?

        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
    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的相同数据保存在字符串中,然后我将一个活动发送到另一个活动。 提前谢谢。。!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Remees M Syde    7 年前

    putParcelableArrayListExtra 而不是 putExtra

    Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
    mIntent.putParcelableArrayListExtra("Data", mArraylist);
    startActivity(mIntent);
    

    或者

    尝试如下使用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);