Java 如何将领域结果中的数据传递到数组列表?
我已经执行了一个领域事务,并为我的数据模型传递了一个json数组Java 如何将领域结果中的数据传递到数组列表?,java,android,realm,Java,Android,Realm,我已经执行了一个领域事务,并为我的数据模型传递了一个json数组 public void execute(Realm realm){ for (int i = 0; i<menuListArray.length(); i++ ){ try{ JSONObject jObject = menuListArray.getJSONObject(i); user.setMenuTitle(jObject.getString("
public void execute(Realm realm){
for (int i = 0; i<menuListArray.length(); i++ ){
try{
JSONObject jObject = menuListArray.getJSONObject(i);
user.setMenuTitle(jObject.getString("title"));
realm.copyToRealm(user);
Intent intent = new Intent(getApplication(), ProfileLandingActivity.class);
startActivity(intent);
finish();
} catch (Exception e){
e.printStackTrace();
}
}
}
现在,如果我执行users.first()
我只能从JSON获取第一个数据,如果我执行users.last()
我只能从JSON获取最后一个数据。但是我必须从领域结果用户那里获取所有数据。如何从JSON获取所有数据?我必须将这些数据传递到数组列表中
请原谅,如果我的任何陈述在我试图成为开发者语言时没有达到开发者语言的要求。您必须在for循环之外使用Intent,如下所示
public void execute(Realm realm){
for (int i = 0; i<menuListArray.length(); i++ ){
try{
JSONObject jObject = menuListArray.getJSONObject(i);
user.setMenuTitle(jObject.getString("title"));
realm.copyToRealm(user);
} catch (Exception e){
e.printStackTrace();
}
}
Intent intent = new Intent(getApplication(), ProfileLandingActivity.class);
startActivity(intent);
finish();
}
公共void执行(领域){
对于(int i=0;i
然后在其他活动中,使用带有的RecyclerView显示元素
RecyclerView.Adapter Adapter=new realmrecyclerviewapter(getContext(),RealmController.initialize(this).getUserMenu(),true){
@凌驾
公共用户ViewHolder onCreateViewHolder(视图组父级,int-viewType){
返回新的UserViewHolder(LayoutInflater.from(parent.getContext())。充气(R.layout.view\u user\u item,parent,false));
}
@凌驾
public void onBindViewHolder(UserViewHolder,int位置){
User=getData().get(位置);
holder.menuTitle.setText(user.getMenuTitle());
}
};
recyclerView.setAdapter(适配器);
尝试通过intent传递菜单标题字符串数组列表。我可以这样做,但出于以后的数据目的,我可能必须使用realm,对吗?请检查copyToRealm重载方法:,我认为它允许您复制多对象可编辑列表。什么是RealmController
?一个单独的类文件,用于处理与函数要求有关的所有领域查询。谢谢@sagar Pithiya因为我是新开发人员,请您建议将所有“标题”数据传递到一个数组中。如果您将所有标题读取到一个数组中,您将丢失自动更新和延迟评估。-\u使用RecyclerView和RealmRecyclerViewAdapter
RealmResults<User> users = RealmController.initialize(this).getUserMenu();
public void execute(Realm realm){
for (int i = 0; i<menuListArray.length(); i++ ){
try{
JSONObject jObject = menuListArray.getJSONObject(i);
user.setMenuTitle(jObject.getString("title"));
realm.copyToRealm(user);
} catch (Exception e){
e.printStackTrace();
}
}
Intent intent = new Intent(getApplication(), ProfileLandingActivity.class);
startActivity(intent);
finish();
}
for (User userData : users ) {
String title = userData.getMenuTitle();
}
public void execute(Realm realm){
for (int i = 0; i < menuListArray.length(); i++) {
try{
JSONObject jObject = menuListArray.getJSONObject(i);
user.setMenuTitle(jObject.getString("title"));
realm.insert(user);
} catch (Exception e){
e.printStackTrace();
}
}
Intent intent = new Intent(getApplication(), ProfileLandingActivity.class);
startActivity(intent);
finish();
}
RecyclerView.Adapter adapter = new RealmRecyclerViewAdapter<User, UserViewHolder>(getContext(), RealmController.initialize(this).getUserMenu(), true) {
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new UserViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_user_item, parent, false));
}
@Override
public void onBindViewHolder(UserViewHolder holder, int position) {
User user = getData().get(position);
holder.menuTitle.setText(user.getMenuTitle());
}
};
recyclerView.setAdapter(adapter);