2016-12-30 4 views
1

私はFirebaseデータベースから移入リストビューを持っている:移入のListView

これは私のコードです:

final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(".../comunidades"); 

    FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
      this.getActivity(), 
      String.class, 
      android.R.layout.simple_list_item_1, 
      databaseReference 
    ) { 
     @Override 
     protected void populateView(View v, String model, int position) { 
      TextView textView = (TextView) v.findViewById(android.R.id.text1); 
      textView.setText(model); 
      mProgress.dismiss(); 

     } 
    }; 

そしてこれは、このデータベース、ブランチのためFirebaseコンソールです: "comunidades":

enter image description here

しかし、私は同じDATから別のブランチからのオブジェクトとリストビューを移入するために別のフラグメントで同じコードを使用していますしかし、私はエラーが発生します。

これは新しいコードです:

final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(".../Enclaves"); 

    FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
      this.getActivity(), 
      String.class, 
      android.R.layout.simple_list_item_1, 
      databaseReference 
    ) { 
     @Override 
     protected void populateView(View v, String model, int position) { 
      TextView textView = (TextView) v.findViewById(android.R.id.text1); 
      textView.setText(model); 


     } 
    }; 

私はブランチ「飛び地」からキー「Nombre_enclave」の値を取得するために何を変更する必要があります。

enter image description here

+2

エラーは何ですか? @NewAtFirebase – Lyla

+0

あなたのFirebaseListAdapterは文字列を保持できません。 Firebaseに格納されているモデルにマップされるオブジェクトである必要があります。 'Nombre_enclave'の文字列フィールドを持つオブジェクト –

+0

@NewAtFirebase:私は以下の答えを提供しました。しかし、今後の質問では、スクリーンショットの代わりにJSONをテキストとして提供してください。 FirebaseデータベースコンソールのExport JSONリンクをクリックすると、簡単にテキストを取得できます。 JSONをテキストとして使用すると、検索可能になり、コメントや回答にJSONを簡単に使用できます。 –

答えて

4

あなたは下に持っている何enclavesはString + Stringペアのリストではありません。その代わりに、文字列+オブジェクトのペアのリストは、この作品を作るための最も簡単な方法は、あなたが表示し、上書きしたいプロパティを決定するなど

Comunidad_enclave、Descripcion_enclaveを持つ各オブジェクトと、されていparseSnapshot()

FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
     this.getActivity(), 
     String.class, 
     android.R.layout.simple_list_item_1, 
     databaseReference 
) { 
    @Override 
    protected String parseSnapshot(DataSnapshot snapshot) { 
     return snapshot.child("Comunidad_enclave").getValue(String.class); 
    } 

    @Override 
    protected void populateView(View v, String model, int position) { 
     TextView textView = (TextView) v.findViewById(android.R.id.text1); 
     textView.setText(model); 
    } 
}; 

これを解決するより良い方法は、各コミニュダードを表すクラスを作成することです。このようになります。最も単純な形ではこのクラスでは

class Comunidad { 
    public String Comunidad_enclave; 
    public String Descripcion__enclave; 
    // TODO: the same for the other properties 
} 

、あなたがタイプComunidadのアダプタを行うことができます。

FirebaseListAdapter<Comunidad> firebaseListAdapter = new FirebaseListAdapter<Comunidad>(
     this.getActivity(), 
     String.class, 
     android.R.layout.simple_list_item_2, 
     databaseReference 
) { 
    @Override 
    protected void populateView(View v, Comunidad model, int position) { 
     TextView textView1 = (TextView) v.findViewById(android.R.id.text1); 
     textView1.setText(model.Comunidad_enclave); 
     TextView textView2 = (TextView) v.findViewById(android.R.id.text2); 
     textView2.setText(model.Descripcion_enclave); 
    } 
}; 
+0

それは動作します、ありがとう、しかし、私はそれがどのように動作するか分かりません。メソッドparseSnapShotはどのようにジョブを作成しますか? – NewAtFirebase

+0

'parseSnapshot()'をオーバーライドしないと、アダプタは自動的に 'DataSnapshot'をコンストラクタで指定したクラス(あなたの場合は' String')に変換します。 2番目のケースではアイテムが単純な文字列ではないので、その変換は何も結果にはなりません。 'parseSnapshot()'をオーバーライドすることによって、 'populateView()'に渡されるデータを制御することができます。驚くほど小さいアダプタのコードを確認してください。関連する部分は次のとおりです。https://github.com/firebase/FirebaseUI-Android/blob/master/database/src/main/java/com/firebase/ui/database/FirebaseListAdapter.java#L118 –

+0

ありがとう努力と努力。私はここにあなたの貢献を見てきました。そして、私は彼らに多くのことを学んでいます。 – NewAtFirebase