2016-08-03 45 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(); 
}