我目前有一个带有listView的片段,我希望用Realm中的记录填充它。在我的应用程序的第一次运行时,没有记录,所以我创建了一个asynctask来从JSON文件填充数据库。
问题是如果对asynctask调用new MuscleTask().execute();
进行注释,则不会找到任何记录,就好像事务没有被持久化一样。所以,每当我在更新代码时在手机上运行应用程序时,数据库都会被刷新,或者我在这里做错了什么?
我是否也应该在asynctask中执行此操作,或者我只需要创建RealmAsyncTransaction
然后使用RealmChangeListener
执行某些操作?
我的代码没有抛出任何异常。
public static class LandingWorkoutDirectoryFragment extends BaseFragment {
private RealmResults<Muscle> muscles;
private MuscleAdapter muscleAdapter;
private ListView listView;
public LandingWorkoutDirectoryFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static LandingWorkoutDirectoryFragment newInstance() {
LandingWorkoutDirectoryFragment fragment = new LandingWorkoutDirectoryFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
muscles = getRealm().allObjects(Muscle.class);
new MuscleTask().execute();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_landing_workout_directory, container, false);
listView = (ListView) rootView.findViewById(R.id.listview_display_exercises);
muscleAdapter = new MuscleAdapter(getActivity(), muscles, true);
listView.setAdapter(muscleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("Di click", String.valueOf(position));
Intent intent = new Intent(getActivity(), DisplayExerciseActivity.class);
intent.putExtra("muscleId", muscles.get(position).getId());
intent.putExtra("muscleName", muscles.get(position).getName());
startActivity(intent);
}
});
return rootView;
}
private class MuscleTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Realm realm = Realm.getDefaultInstance();
InputStream is = null;
try {
is = getActivity().getAssets().open("muscles.json");
realm.beginTransaction();
realm.createOrUpdateAllFromJson(Muscle.class, is);
realm.commitTransaction();
} catch (IOException e) {
realm.cancelTransaction();
}
return null;
}
}
}
这是我的肌肉适配器
public class MuscleAdapter extends RealmBaseAdapter<Muscle> implements ListAdapter {
static class ViewHolder {
@Bind(R.id.firstLine) TextView name;
@Bind(R.id.secondLine) TextView description;
@Bind(R.id.icon) ImageView image;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
public MuscleAdapter(Context context, RealmResults<Muscle> realmResults, boolean automaticUpdate) {
super(context, realmResults, automaticUpdate);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listview_item_exercise, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Muscle item = realmResults.get(position);
viewHolder.name.setText(item.getName());
viewHolder.description.setText(item.getDescription());
Glide.with(parent.getContext()).load(item.getImage()).into(viewHolder.image);
return convertView;
}
}
编辑:这是我的基础适配器。
public class BaseFragment extends Fragment {
public final String log = this.getClass().getSimpleName();
public Realm realm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
realm = Realm.getDefaultInstance();
}
@Override
public void onDestroy() {
super.onDestroy();
realm.close();
}
public Realm getRealm() {
return realm;
}
}
编辑2:添加了我的应用程序应用程序类
public class FititApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Configure Realm for the application
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
Realm.deleteRealm(realmConfiguration); // Clean slate
Realm.setDefaultConfiguration(realmConfiguration); // Make this Realm the default
}
}
答案 0 :(得分:0)
如@AdamS所述,Realm.deleteRealm()
类FititApplication
上的这一行Application
来自@vat_rate
,这是我的问题。评论这行代码是解决我的问题所需的全部内容。这行代码每次都丢失了我的数据库。