2017-09-06 16 views
0

私は最初のfirebaseアンドロイドアプリケーションに取り組んでいて、ほとんど完了しましたが、データベースからデータを取得するのに苦労しました。データの追加と更新はうまくいきます。 それはshowData方法のforloopに入っていない私のviewInformationレイアウト(ログの猫を使用してチェック) では、私がログインしている特定のユーザーにデータを表示したい。(唯一の彼/彼女のデータが表示されます)Firebaseの特定のユーザーデータをAndroidのListViewに表示するにはどうすればいいですか?

ここに私のFirebase DBの構造は次のとおりです。 my Firebase DB structure

表示情報コード:

public class ViewInformation extends AppCompatActivity { 

private static final String TAG = "ViewInformation"; 
private FirebaseDatabase mFirebaseDatabase; 
private FirebaseAuth auth; 
private FirebaseAuth.AuthStateListener authListener; 
private DatabaseReference mRef; 
String userID; 
private ListView mListView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_view_information); 
    Toast.makeText(getApplicationContext(), "Showing Details...", Toast.LENGTH_SHORT).show(); 
    mListView = findViewById(R.id.listview); 

    auth = FirebaseAuth.getInstance(); 
    mFirebaseDatabase = FirebaseDatabase.getInstance(); 
    mRef = mFirebaseDatabase.getReference(); 
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); //get current user 
    userID = user.getUid(); //get userID of current user 

    authListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      FirebaseUser user = firebaseAuth.getCurrentUser(); 
      if (user == null) { 
       // user auth state is changed - user is null ...launch login activity 
       startActivity(new Intent(ViewInformation.this, LoginActivity.class)); 
       finish(); 
      } 
     } 
    }; 


     mRef.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       Toast.makeText(getApplicationContext(), "Showing Details...", Toast.LENGTH_SHORT).show(); 
       showData(dataSnapshot); 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
      } 

     }); 
} 

private void showData(DataSnapshot dataSnapshot) { 
    Toast.makeText(getApplicationContext(), "...", Toast.LENGTH_SHORT).show(); 
    for(DataSnapshot ds:dataSnapshot.getChildren()){ 

     userInformation uInfo = new userInformation(); 
     uInfo.setName(ds.child(userID).child("Name").getValue(userInformation.class).getName()); 
     uInfo.setGender(ds.child(userID).child("Gender").getValue(userInformation.class).getGender()); 
     uInfo.setMobile_Number(ds.child(userID).child("Mobile_Number").getValue(userInformation.class).getMobile_Number()); 
     uInfo.setAddress(ds.child(userID).child("Address").getValue(userInformation.class).getAddress()); 

     Log.d(TAG, "showData: Name: " + uInfo.getName()); 
     Log.d(TAG, "showData: Gender: " + uInfo.getGender()); 
     Log.d(TAG, "showData: Mobile_Number: " + uInfo.getMobile_Number()); 
     Log.d(TAG, "showData: Address: " + uInfo.getAddress()); 

     ArrayList<String> array = new ArrayList<>(); 
     array.add(uInfo.getName()); 
     array.add(uInfo.getGender()); 
     array.add(uInfo.getMobile_Number()); 
     array.add(uInfo.getAddress()); 
     ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,array); 
     mListView.setAdapter(adapter); 
    } 
} 


@Override 
public void onStart() { 
    super.onStart(); 
    auth.addAuthStateListener(authListener); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    if (authListener != null) { 
     auth.removeAuthStateListener(authListener); 
    } 
} 
} 

getter/setterメソッド:(userInformation.java)

package com.scoratech.scoraxchange; 
public class userInformation { 

private String Name; 
private String Gender; 
private String Mobile_Number; 
private String Address; 

public userInformation(){ 
} 

public String getName() { 
    return Name; 
} 

public void setName(String name) { 
    this.Name = name; 
} 

public String getGender() { 
    return Gender; 
} 

public void setGender(String gender) { 
    this.Gender = gender; 
} 

public String getMobile_Number() { 
    return Mobile_Number; 
} 

public void setMobile_Number(String mobile_Number) { 
    this.Mobile_Number = mobile_Number; 
} 

public String getAddress() { 
    return Address; 
} 

public void setAddress(String address) { 
    this.Address = address; 
} 
} 

答えて

0

あなたはmFirebaseDatabase.getReference("list-abc05")で、存在しないノードを取得しています。プロジェクトのデータベースのルートがそう、無名のです。また

mRef = mFirebaseDatabase.getReference(); 

、私は、ユーザーが(onAuthStateChanged内からそう)認証された後にのみ、リスナーを取り付けるお勧めします。あなたはすでにユーザーのUIDを知っているので、(すべてのユーザーのデータを取得するのではなく)自分のノードに直接アタッチできます:

final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();  
authListener = new FirebaseAuth.AuthStateListener() { 
    @Override 
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
     FirebaseUser user = firebaseAuth.getCurrentUser(); 
     if (user == null) { 
      startActivity(new Intent(ViewInformation.this, LoginActivity.class)); 
      finish(); 
     } 
     else { 
      userID = user.getUid(); 
      mRef.child(userID).addValueEventListener(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 
        Toast.makeText(getApplicationContext(), "Showing Details...", Toast.LENGTH_SHORT).show(); 
        showData(dataSnapshot); 
       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) { 
        throw databaseError.toException(); // never ignore errors 
       } 
      }); 
     } 
    } 
}; 

private void showData(DataSnapshot ds) { 

    userInformation uInfo = new userInformation(); 
    uInfo.setName(ds.child(userID).child("Name").getValue(userInformation.class).getName()); 
    uInfo.setGender(ds.child(userID).child("Gender").getValue(userInformation.class).getGender()); 
    uInfo.setMobile_Number(ds.child(userID).child("Mobile_Number").getValue(userInformation.class).getMobile_Number()); 
    uInfo.setAddress(ds.child(userID).child("Address").getValue(userInformation.class).getAddress()); 

    Log.d(TAG, "showData: Name: " + uInfo.getName()); 
    Log.d(TAG, "showData: Gender: " + uInfo.getGender()); 
    Log.d(TAG, "showData: Mobile_Number: " + uInfo.getMobile_Number()); 
    Log.d(TAG, "showData: Address: " + uInfo.getAddress()); 

    ArrayList<String> array = new ArrayList<>(); 
    array.add(uInfo.getName()); 
    array.add(uInfo.getGender()); 
    array.add(uInfo.getMobile_Number()); 
    array.add(uInfo.getAddress()); 
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,array); 
    mListView.setAdapter(adapter); 
} 
+0

ありがとうございます。 DB参照について私は後で変更した特定のルートノードを与えようとしていました。私はあなたのコードを試しましたが、それは動作しませんし、変数 'ds'をforループで初期化していません。 – KhanStan99

+0

ああ、私は議論の名前を変更しませんでした。今はしました。 –

+0

私はエラーがshowDataメソッドにあると思う、私はルートノードchild(すべてのユーザの一意のID)にアクセスしたいが、私のコードはルートノードchildの子にアクセスしている。 – KhanStan99

関連する問題