Android 如何仅在RealmResults为空时可靠地为RecyclerView显示空视图
我知道有很多关于它的信息,但我还没有找到一个与我的情况相匹配的 我有一个始终打开的片段的循环视图,因此片段基本上不会重新创建自己 这是我加载适配器的代码Android 如何仅在RealmResults为空时可靠地为RecyclerView显示空视图,android,android-fragments,realm,Android,Android Fragments,Realm,我知道有很多关于它的信息,但我还没有找到一个与我的情况相匹配的 我有一个始终打开的片段的循环视图,因此片段基本上不会重新创建自己 这是我加载适配器的代码 reLoad(); //method shown below mRecycler.setAdapter(new SolicitationAdapter(myRealm.where(SolicitationDatabase.class).findAllAsync())); 这就是我提出的逻辑: public void reLoa
reLoad(); //method shown below
mRecycler.setAdapter(new SolicitationAdapter(myRealm.where(SolicitationDatabase.class).findAllAsync()));
这就是我提出的逻辑:
public void reLoad() {
if (!myRealm.where(SolicitationDatabase.class).findAll().isEmpty()) {
mNothingHere.setVisibility(View.GONE);
mRecycler.setVisibility(View.VISIBLE);
} else {
mNothingHere.setVisibility(View.VISIBLE);
mRecycler.setVisibility(View.GONE);
}
}
用户第一次打开应用程序时,它的效果非常好
当用户创建一条记录时,问题就开始了,因为片段不会重新创建自己,也不会重新加载
我无法在用户添加内容后重新加载的原因是,添加新记录的方法位于从不同活动调用的单例上。这意味着,当我尝试这样做时,在声明recycleview和textview时,会得到一个nullpointerexception
编辑-我尝试的内容(从其他位置重新加载视图)
我有一个叫做PostHelper的类,这个类负责发布新记录
这是构造器:
public PostHelper(Context context, Activity activity) {
this.mContext = context;
this.mActivity = activity; //I call this in order to use "findViewById"
这就是post发生的地方:
public String addSolicitation(File _file, boolean fromQueue) {
//loading view
TextView nothingHere = (TextView) mActivity.findViewById(R.id.nothing_here);
RecyclerView recycler = (RecyclerView) mActivity.findViewById(R.id.recycler);
...so on until after the post:
SolicitationAdapter n = new SolicitationAdapter(myRealm.where(SolicitationDatabase.class).findAll());
n.notifyDataSetChanged();
nothingHere.setVisibility(View.GONE);
recycler.setVisibility(View.VISIBLE);
这是stacktrace:
06-01 21:43:37.511 9122-9122/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ga.realm3, PID: 9122
io.reactivex.exceptions.OnErrorNotImplementedException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference
编辑2-我使用以下命令加载PostHelper类:
如果我理解正确,您希望在其他地方发生操作时在另一个视图中得到通知 实现这一点的方法通常是使用接口
public class PostHelper {
// Define these
public interface PostActionListener {
void onPostAdded();
}
private PostActionListener postListener;
public void setPostActionListener(PostActionListener listener) throws ClassCastException {
this.postListener = (PostActionListener) context;
}
public PostHelper(Context context) {
this.mContext = context;
// Cast the passed context as a listener
if (mContext instanceof PostActionListener) {
this.postListener = (PostActionListener) mContext;
}
}
public String addSolicitation(File _file, boolean fromQueue) {
// Do something
// Callback to the UI to update
if (this.postListener != null) {
this.postListener.onPostAdded();
}
}
然后,在你最初的活动中
public class YourActivity extends AppCompatActivity
implements PostHandler.PostActionListener {
// ... fields
@Override
public void onPostAdded() {
reLoad();
}
public void reLoad() {
boolean emptyList = myRealm.where(SolicitationDatabase.class).findAll().isEmpty();
mNothingHere.setVisibility(emptyList ? View.VISIBLE : View.GONE);
mRecycler.setVisibility(emptyList ? View.GONE : View.VISIBLE);
}
@Override
public void onCreate(Bundle b) {
...
mPostHelper = new PostHelper(this);
}
但是,由于您使用的是Realm,并且此处确实没有需要“返回”的数据,因此您可以简单地让Android活动生命周期为您刷新数据 您应该确保
requisitionadapter
是realmrecycleServiceAdapter
,如下所示:
public class SolicitationAdapter extends RealmRecyclerViewAdapter<SolicitationDatabase, SolicitationViewHolder> {
public SolicitationAdapter(OrderedRealmCollection<SolicitationDatabase> results) {
super(results, true);
}
...
}
公共类请求适配器扩展RealmRecyclServiceAdapter{
公开征求适配器(OrderedRealmCollection结果){
超级(结果,正确);
}
...
}
然后,您需要做的是将RealmResults作为活动中的字段引用:
public class PostSoliticiationActivity extends AppCompatActivity {
RealmResults<Solicitation> results;
Realm realm;
RealmChangeListener<RealmResults<Solicitiation> realmChangeListener = (results) -> {
if(results.isLoaded() && results.isValid()) {
if(results.isEmpty()) {
mNothingHere.setVisibility(View.GONE);
mRecycler.setVisibility(View.VISIBLE);
} else {
mNothingHere.setVisibility(View.VISIBLE);
mRecycler.setVisibility(View.GONE);
}
}
}
SolicitationAdapter adapter;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.soliticiation_activity);
// bind views
realm = Realm.getDefaultInstance();
results = realm.where(SolicitationDatabase.class).findAllSortedAsync("id");
// .sort("id").findAllAsync(); in 4.3.0+
results.addChangeListener(realmChangeListener);
adapter = new SoliticiationAdapter(results);
mRecycler.setAdapter(adapter);
// layout manager as well
}
@Override
public void onDestroy() {
results.removeChangeListener(realmChangeListener);
realm.close();
super.onDestroy();
}
}
公共类PostSoliticationActivity扩展了AppCompatitionActivity{
真实结果;
境界;
RealmChangeListener{
if(results.isload()&&results.isValid()){
if(results.isEmpty()){
mNothingHere.setVisibility(View.GONE);
mRecycler.setVisibility(View.VISIBLE);
}否则{
mNothingHere.setVisibility(View.VISIBLE);
mRecycler.setVisibility(View.GONE);
}
}
}
请求适配器;
@凌驾
创建时受保护的void(捆绑){
super.onCreate(bundle);
setContentView(R.layout.Solicitation_活动);
//绑定视图
realm=realm.getDefaultInstance();
结果=realm.where(requisitionDatabase.class).findAllSortedAsync(“id”);
//.sort(“id”).findalsync();在4.3.0中+
结果:addChangeListener(realmChangeListener);
适配器=新的孤子适配器(结果);
mRecycler.setAdapter(适配器);
//布局管理器也是如此
}
@凌驾
公共空间{
结果:removeChangeListener(realmChangeListener);
realm.close();
super.ondestory();
}
}
所以你不需要的东西: 1.
reLoad()
方法
2.onPostAdded
callback
3.PostActionListener
只要您在事务中将
SoliciationDatabase
添加到域中,就可以在不手动同步ui的情况下工作。请显示stacktrace和其他code@cricket_007谢谢你指出这一点。我已经用所需的信息编辑了它。你能帮我吗?如果你是“从一个不同的活动”更新领域,那么你不应该在那里传递另一个活动的全部引用。。。另外,活动是一个上下文,所以您只需复制参数。。。否则,我无法真正理解您添加的代码,但是调用findViewById
并不是这样工作的。您是否尝试只传递TextView&RecyclerView?假设你真的需要它们?我将要测试它,顺便说一句,适配器实际上是你自己做的(通过例子领域书)!你总是帮我,我真的很感激。这是一个很好的策略。然而,唯一阻止它工作的是我不能绑定PostRequestActivity的视图。它们属于历史片段。Nevermind在HistoryFragment上做了这件事,它成功了。谢谢。我在底部添加了免责声明:)我基本上手动实现了RealmChangeListener
public class PostSoliticiationActivity extends AppCompatActivity {
RealmResults<Solicitation> results;
Realm realm;
RealmChangeListener<RealmResults<Solicitiation> realmChangeListener = (results) -> {
if(results.isLoaded() && results.isValid()) {
if(results.isEmpty()) {
mNothingHere.setVisibility(View.GONE);
mRecycler.setVisibility(View.VISIBLE);
} else {
mNothingHere.setVisibility(View.VISIBLE);
mRecycler.setVisibility(View.GONE);
}
}
}
SolicitationAdapter adapter;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.soliticiation_activity);
// bind views
realm = Realm.getDefaultInstance();
results = realm.where(SolicitationDatabase.class).findAllSortedAsync("id");
// .sort("id").findAllAsync(); in 4.3.0+
results.addChangeListener(realmChangeListener);
adapter = new SoliticiationAdapter(results);
mRecycler.setAdapter(adapter);
// layout manager as well
}
@Override
public void onDestroy() {
results.removeChangeListener(realmChangeListener);
realm.close();
super.onDestroy();
}
}