2016-08-03 26 views
3

问题FirebaseRecyclerAdapter - 如何创建

我有通过一个FirebaseRecyclerAdapter显示在Android的项目列表后更改数据库查询。我使用所有项目列表上的数据库引用创建了这个适配器(比方说“posts”)。该帖子还显示类别,如果用户点击该类别,我想更新列表以仅显示该类别上的项目。

解决方案我试过不行 我把一个点击监听器上的“类别”的TextView,当它在用户点击我试图实例化新的数据库查询的新适配器,但它不工作。

其他的想法: 做到这一点adpater外: 创建一个新的意图重振片段,并与新的查询中创建一个新的适配器

问题 是否有人处理同样的情况在用户点击你想过滤由adpater检索的项目的原始列表? 什么是最佳解决方案?

详细

protected void populateViewHolder(final PostViewHolder viewHolder, final Post post, final int position) { 


    /* Set the post text, author and category */ 
    viewHolder.postText.setText(post.getText()); 
    viewHolder.postAuthor.setText(post.getAuthor()); 
    viewHolder.postCategory.setText(post.getCategory()); 

    /* Set a listener on category so that 
     I refresh data with only that category */ 
    viewHolder.quoteCategory.setOnClickListener(new View.OnClickListener() { 

     Post selectedPost = getItem(position); 

     @Override 
     public void onClick(View v) { 

      String category = selectedPost.getCategory(); 

      // !!!! NOW I HAVE TO CHANGE DATA SET !!! /// 

我想现在我可以创建与正确的DB查询一个新的适配器一个新的片断......但是这是最好的办法吗? 有没有办法在填充视图内使用同一个Adapater传递另一个查询?

回答

1

尝试在点击发生时在适配器上调用.notifyDatasetChanged()。用新的Firebase数据更改数据集后。

+0

感谢您的回答,但问题不在于通知数据集的更改......问题是如何在创建完adpater后更改数据集。 “ ”在您更改数据集后“...我该怎么做? – DavideN

0

以下是更新Firebase数据的代码。这里我更新了两个参数,传递给函数作为标题和正文。

private void writeNewPost(String userId, String username, String title, String body) { 

     String key = mDatabase.child("posts").push().getKey(); 
     Post post = new Post(userId, username, title, body); 
     Map<String, Object> postValues = post.toMap(); 

     Map<String, Object> childUpdates = new HashMap<>(); 
     childUpdates.put("/posts/" + key, postValues); 
     childUpdates.put("/user-posts/" + userId + "/" + key, postValues); 

     mDatabase.updateChildren(childUpdates); 
    } 

我想这会帮助你。

+0

感谢您的回答,我不想更新/更改数据...我想在UI中以不同的方式对它们进行过滤。 我发现它的解决方案是创建一个新的片段,并用一个新的firebaseadapter调用它,在这里我将另一个firebase查询放在构造函数中 – DavideN

1

您不会更改查询,您将创建新的适配器和查询,在回收站视图中替换适配器,并清理旧的适配器。

下面是一个例子,“更改查询”的基础上在旋转的选择:

locationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(final AdapterView<?> adapterView, final View view, 
     final int i, final long l) { 
      if (locationSpinner.getSelectedItem() != null) { 
       final Location location = locationSpinner.getSelectedItem(); 
       final String indexString = String.format("open/%s/", location.getId()); 
       final Query query = FirebaseDatabase.getInstance() 
         .getReference("availabilities") 
         .orderByChild("statusLocationDateKey") 
         .startAt(indexString + "0") // simplified for example 
         .endAt(indexString + "9"); // simplified for example 
       adapter = new ListingAdapter(query); 
       final FirebaseRecyclerAdapter oldAdapter = 
         (FirebaseRecyclerAdapter) listing.getAdapter(); 
       listing.setAdapter(adapter); 
       if (oldAdapter != null) { 
        oldAdapter.cleanup(); 
       } 
      } 
     } 

注:ListingAdapterFirebaseRecyclerAdapterlisting一个子类是RecyclerView

此外,一定要包括一个空检查的破坏 的活动或片段,因为上面的代码,也不能保证该适配器是有史以来初始化,例如:

@Override 
public void onDestroyView() { 
    if (adapter != null) { 
     adapter.cleanup(); 
    } 
    super.onDestroyView(); 
} 
相关问题