gpt4 book ai didi

android - 如何使用 Realm 将预加载数据显示到 RecyclerView

转载 作者:行者123 更新时间:2023-11-30 00:25:14 26 4
gpt4 key购买 nike

在我的应用程序中,我使用 Realm 作为本地数据库。我正在使用 recyclerview 小部件来显示这些数据。现在最初我想在回收站 View 中显示一些预加载数据,这些数据也将存储在 Realm 中。然后我将实现添加、编辑、删除方法。但是我在尝试运行这个应用程序时遇到了 fatal error 。我是 Realm 的新手。我无法确定这是哪个问题。

我已经借助答案代码解决了这个问题。这是解决方案。

已解决的代码

我的 Activity 类是

public class MyColleaguesPage extends AppCompatActivity {

private RecyclerView recyclerView;
private MyColleaguesAdapter adapter;
private Realm colleagueRealm;
private List<MyColleagueModel> colleagueObject;
private RealmResults<MyColleagueModel> dataResult;
private static final String DIALOG_TAG = "EmployeeDialog";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);

colleagueRealm = Realm.getDefaultInstance();
recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);

setUpRecycler();

if (!Prefs.with(this).getPreLoad()) {
setRealmData();
}
showAllPersons();

}

private void showAllPersons() {
dataResult = colleagueRealm.where(MyColleagueModel.class).findAll();
setAdapter(dataResult);
adapter.notifyDataSetChanged();
}
//find all objects in the Book.class
private void setAdapter(RealmResults<MyColleagueModel> results) {
adapter = new MyColleaguesAdapter(this, results);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();

}
private void setUpRecycler() {

recyclerView.setHasFixedSize(true);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
}

private void setRealmData(){

List<MyColleagueModel> colleague = new ArrayList<>();
MyColleagueModel model = new MyColleagueModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
colleague.add(model);

model = new MyColleagueModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Comapny2");
model.setTitle("Title1");
colleague.add(model);

model = new MyColleagueModel();
model.setId(3 + System.currentTimeMillis());
model.setName("Name3");
model.setCompany("Comapny3");
model.setTitle("Title3");
colleague.add(model);


for (MyColleagueModel realmModel : colleague) {
// Persist the colleague data
colleagueRealm.beginTransaction();
colleagueRealm.copyToRealm(realmModel);
colleagueRealm.commitTransaction();
}

Prefs.with(this).setPreLoad(true);

}

.....

最佳答案

解决眼前的问题

private void setRealmData(){
List<MyColleagueModel> colleague = new ArrayList<>();

MyColleagueModel model = new MyColleagueModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
colleague.add(model);

model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Comapny2");
model.setTitle("Title1");
colleague.add(model);

model.setId(3 + System.currentTimeMillis());
model.setName("Name3");
model.setCompany("Comapny3");
model.setTitle("Title3");
colleague.add(model);

应该是

private void setRealmData(){
List<MyColleagueModel> colleague = new ArrayList<>();

MyColleagueModel model = new MyColleagueModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
colleague.add(model);

model = new MyColleagueModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Comapny2");
model.setTitle("Title1");
colleague.add(model);

model = new MyColleagueModel();
model.setId(3 + System.currentTimeMillis());
model.setName("Name3");
model.setCompany("Comapny3");
model.setTitle("Title3");
colleague.add(model);

但基于 Prefs.with(this).preload(),您的代码基于 Ravi Tamada 的教程,该教程充满了糟糕的做法,我认为它仍然是 Realm 0.82.2。

不要忘记 Realm 的最新版本(在撰写本文时)是 Realm 3.5.0,最好使用 official documentation .

例如,您应该使用 realm-android-adapters 提供的 RealmRecyclerViewAdapter (官方插件)而不是使用你自己的插件。


编辑:这是完整的解决方案

public class MyColleaguesPage extends AppCompatActivity implements ColleagueListListener {

private Realm realm;
private RecyclerView recyclerView;
private MyColleaguesAdapter adapter;

private static final String DIALOG_TAG = "EmployeeDialog";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);

// Showing and Enabling clicks on the Home/Up button
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
realm = Realm.getDefaultInstance();
recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);

recyclerView.setHasFixedSize(true);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter = new MyColleaguesAdapter(this, realm.where(MyColleagueModel.class).findAllSortedAsync("id"));
recyclerView.setAdapter(adapter);
}

@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
realm = null;
}
}

public class InitialData implements Realm.Transaction {
@Override
public void execute(Realm realm) {
List<MyColleagueModel> colleague = new ArrayList<>();
MyColleagueModel model = new MyColleagueModel();
model.setId(1 + System.currentTimeMillis());
model.setName("Name1");
model.setCompany("Comapny1");
model.setTitle("Title1");
colleague.add(model);

model = new MyColleagueModel();
model.setId(2 + System.currentTimeMillis());
model.setName("Name2");
model.setCompany("Comapny2");
model.setTitle("Title1");
colleague.add(model);

model = new MyColleagueModel();
model.setId(3 + System.currentTimeMillis());
model.setName("Name3");
model.setCompany("Comapny3");
model.setTitle("Title3");
colleague.add(model);


for (MyColleagueModel realmModel : colleague) {
realm.insertOrUpdate(realmModel);
}
}

@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof InitialData;
}

@Override
public int hashCode() {
return InitialData.class.hashCode();
}
}

public class CustomApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
Realm.setDefaultConfiguration(new RealmConfiguration.Builder()
.deleteIfMigrationNeeded()
.initialData(new InitialData())
.build());
}
}

<application name=".CustomApplication"
.../>

public class MyColleaguesAdapter extends RealmRecyclerViewAdapter<MyColleagueModel, MyColleaguesAdapter.ColleagueHolder> {
public interface ColleagueListListener {
void addPerson(MyColleagueModel colleagueModel);
void editPerson(MyColleagueModel colleagueModel);
}

private final ColleagueListListener colleagueListListener;

public MyColleaguesAdapter(ColleagueListListener colleagueListListener, RealmResults<MyColleagueModel> results) {
super(results, true);
this.colleagueListListener = colleagueListListener;
}

@Override
public ColleagueHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ColleagueHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.colleage_row_layout,parent,false));
}

@Override
public void onBindViewHolder(ColleagueHolder holder, int position) {
final MyColleagueModel myColleague = getData().get(position);
holder.bind(myColleague);
}

public class ColleagueHolder extends RecyclerView.ViewHolder {

public CardView cardView;
public ImageView colleaguePicture;
public TextView colleagueName;
public TextView companyName;
public TextView jobTitle;

public ColleagueHolder(View itemView) {
super(itemView);
//colleaguePicture=(ImageView)itemView.findViewById(R.drawable.profile_image);
colleagueName=(TextView)itemView.findViewById(R.id.colleague_name);
companyName=(TextView) itemView.findViewById(R.id.company_name);
jobTitle=(TextView) itemView.findViewById(R.id.job_title);
cardView=(CardView)itemView.findViewById(R.id.cardview_user);
}
}

public void bind(MyColleagueModel myColleague) {
holder.colleagueName.setText(myColleague.getName());
holder.companyName.setText(myColleague.getCompany());
holder.jobTitle.setText(myColleague.getTitle());
holder.cardView.setTag(position);
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
colleagueListListener.editPerson(myColleague);
}
});
}
}

关于android - 如何使用 Realm 将预加载数据显示到 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45506203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com