2017-12-27 5 views
0

私はFirestoreRecyclerAdapterを使用しています。Firestore dbでAndroidの文書IDや名前を取得して別のアクティビティに渡す方法は?

ドキュメントの各モデルと属性を取得できます。しかし、私は文書IDを取得したい。

私はmodel. Autosuggestionsを使用してみましたが、何の文書IDまたは名前

は、私は、次のDealsHolderクラスを持っていませんでした。これはKotlinにある。しかし、すべてのクラスの残りの部分はjavaになっています。私はのonCreate()で初期化

package com.guidoapps.firestorerecycleradaptersample 


import com.google.firebase.firestore.IgnoreExtraProperties 

@IgnoreExtraProperties 
data class DealsResponse(var title:String? = null, 
       var price:String? = null, 
       var dealPrice:String? = null, 
       var image:String? = null, 
       var description: String? = null) 

次の関数

私は別のアクティビティ

に合格するには、ドキュメントのIDを取得したい。ここholder.itemView onClickListenerで

private void getDealsList(){ 
     Query query = db.collection("deals").orderBy("dateTime", Query.Direction.DESCENDING); 

     FirestoreRecyclerOptions<DealsResponse> response = new FirestoreRecyclerOptions.Builder<DealsResponse>() 
       .setQuery(query, DealsResponse.class) 
       .build(); 

     adapter = new FirestoreRecyclerAdapter<DealsResponse, MainActivity.DealsHolder>(response) { 
      @Override 
      public void onBindViewHolder(MainActivity.DealsHolder holder, int position, DealsResponse model) { 
       progressBar.setVisibility(View.GONE); 
       holder.textTitle.setText(model.getTitle()); 
       holder.textPrice.setText(model.getPrice()); 
       holder.textDesc.setText(model.getDescription()); 
       holder.textDealPrice.setText(model.getDealPrice()); 

       holder.textPrice.setPaintFlags(holder.textPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 

       Glide.with(getApplicationContext()) 
         .load(model.getImage()) 
         .into(holder.imageView); 

       holder.itemView.setOnClickListener(v -> { 
        Snackbar.make(DealsList, model.getTitle(), Snackbar.LENGTH_LONG) 
          .setAction("Action", null).show(); 

       }); 
      } 

その後、以下のDealsHolder。 collection/myDoc/someFieldで、myDocがIDになるので、

@Override 
public void onBindViewHolder(MainActivity.DealsHolder holder, int position, DealsResponse model) { 
    // ... 
    holder.itemView.setOnClickListener(v -> { 
     DocumentSnapshot snapshot = getSnapshots().getSnapshot(holder.getAdapterPosition()); 
     snapshot.getId(); 
     // ... 
    }); 
} 

getId()方法は、文書のIDを返します。

public class DealsHolder extends RecyclerView.ViewHolder { 
    @BindView(R.id.title) 
    TextView textTitle; 
    @BindView(R.id.thumbnail) 
    ImageView imageView; 
    @BindView(R.id.description) 
    TextView textDesc; 
    @BindView(R.id.price) 
    TextView textPrice; 
    @BindView(R.id.dealPrice) 
    TextView textDealPrice; 

    public DealsHolder(View itemView) { 
     super(itemView); 
     ButterKnife.bind(this, itemView); 
    } 
} 

答えて

3

は、アダプタのgetSnapshots()メソッドを使用します。

次のアクティビティでデータ構造がわかっている場合は、標準firestore.collection("foo").document("bar")メソッドを使用して、そのIDの参照を再作成できます。あなたは一般的な解決策を探しているなら、私はgetPath()束を使用します。

fun Bundle.putRef(ref: DocumentReference) = putString(REF_KEY, ref.path) 

fun Bundle.getRef() = FirebaseFirestore.getInstance().document(getString(REF_KEY)) 

あなたはモデルのidをしたい場合は、カスタムSnapshotParserを使用します。

val options = FirestoreRecyclerOptions.Builder<DealsResponse>() 
     .setQuery(query) { 
      it.toObject(DealsResponse::class.java).apply { id = it.id } 
     } 
     .build() 
+0

'snapshot.getIdを何種類(); 'return? IDEで自動的にIDEにどのような型が返されるか知る方法はありますか? – Kotlinboy

+0

'String id =(String)snapshot.getId();' 'id'は別のアクティビティをインテントとして渡すことはできますか? – Kotlinboy

+0

これはRecycleの子のIDではなく、データベース内のFireStoreドキュメントのIDです。 Firestore DBコールを使用してアイテムを取得できるように、データベースアイテムIDが必要です。 dbのPKと同様です。 – Kotlinboy

関連する問題