我有一个带有listFragments的应用程序,我希望以更好的方式将Realm中的数据显示在它们中。 例如,我有拥有大量数据的用户(姓名,喜欢的体育等...)
用户类:
public class User extends RealmObject {
@PrimaryKey
private long id;
private String firstName;
private String lastName;
private String email;
private RealmList<UserSport> sports = new RealmList<>();
private RealmList<User> friends= new RealmList<>();
private RealmList<User> sportmates= new RealmList<>();
{and some other, and getters and setters}
}
用户可以选择“用户”菜单,其中有3个ListFragments在不同的标签中(朋友,运动员,其他)
朋友片段:
public class FriendsFragment extends ListFragment {
List<User> users;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_team, container, false);
users=new ArrayList<>();
long userid=/*The currently logged in users id.*/
Realm realm = Realm.getInstance(getContext());
users.addAll(realm.where(User.class).equalTo("id",userid).findFirst().getFriends());
MySportmateListAdapter adapter = new MySportmateListAdapter(getContext(), users);
setListAdapter(adapter);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final ListView listView = getListView();
final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
// swipeRefreshLayout.setOnRefreshListener();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//TODO implement
});
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int topRowVerticalPosition = (listView == null || listView.getChildCount() == 0) ? 0 : listView.getChildAt(0).getTop();
swipeRefreshLayout.setEnabled(firstVisibleItem == 0 && topRowVerticalPosition >= 0);
}
});
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getContext(), "Here is " + position + ". item " + v.getTag(), Toast.LENGTH_LONG).show();
}
MyListAdapter从baseadapter扩展(并实现可过滤,但它用于其他用途,因此现在不需要):
package teammeup.com.teammeup.ListAdapters;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import retrofit.Call;
import retrofit.JacksonConverterFactory;
import retrofit.Retrofit;
import teammeup.com.teammeup.API.RestService;
import teammeup.com.teammeup.R;
import teammeup.com.teammeup.domain.User;
public class MySportmateListAdapter extends BaseAdapter implements Filterable {
Context context;
List<User> users;
ListFragment parentFragment;
public MySportmateListAdapter(Context context, List<User> users) {
super();
this.context = context;
this.users = users;
}
/*This is for the other usage!*/
public MySportmateListAdapter(Context context, ListFragment fragment) {
super();
this.context = context;
parentFragment=fragment;
}
@Override
public int getCount() {
return users.size();
}
@Override
public Object getItem(int position) {
return users.get(position);
}
@Override
public long getItemId(int position) {
return users.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.user_on_list, parent, false);
}
User user = users.get(position);
/*Here are some textviews and other for a custom layout... and I sett hem with the datas from the user.*/
convertView.setTag(user.getId());
return convertView;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
/*For other usage, so it not neccessary now*/
return filter;
}
}
所以我的问题是我想将该适配器用于Sportmates和Others片段,这些片段几乎相同但具有不同的数据。我认为在这种情况下我必须从ListFragments将数据传递给适配器。
我的问题是,如何以更好的方式处理这个领域?在哪里打开/关闭它?我在菜单上有一个注销选项,我想删除所有数据(整个Realm)。
我认为onResume / onPause打开/关闭将无效,因为注销。
我使用slidingtablayout作为选项卡,如果我是正确的,它会在打开的片段附近准备碎片,因此还有另外一个原因可以打开/关闭onResume / onPause。
你能帮助我,以正确的方式做到吗?
答案 0 :(得分:0)
我们最近在我们的文档中添加了一个关于控制Realm生命周期的最佳实践部分:https://realm.io/docs/java/latest/#best-practices
除了尝试在注销时删除Realm文件,您还可以通过执行此处描述的操作来删除Realm中的所有数据:https://github.com/realm/realm-java/issues/1796