2017-12-15 18 views
1

私はユーザーのデータをリストしますが、最後のユーザーのデータをリストします。正しい行数を表示します。5人のユーザーが5行を表示します同じ、最後のユーザー。カスタムArrayAdapterは最後の情報のみを表示します

主な活動はこれです:設について

for(DataSnapshot Ds : dS.getChildren()) { 
Object key = Ds.getKey(); 
String StringKey = String.valueOf(key); 
getname(StringKey); 
} 



private void getname(String ID){ 

    final String IDfinal = ID; 
    DatabaseReference.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      Name = dataSnapshot.child("Users").child(IDfinal).child("name").getValue(String.class); 
      Email = dataSnapshot.child("Users").child(IDfinal).child("email").getValue(String.class); 
      Gender = dataSnapshot.child("Users").child(IDfinal).child("gender").getValue(String.class); 
      Birthday = dataSnapshot.child("Users").child(IDfinal).child("data_birth").getValue(String.class); 
      UserInformation ID = new UserInformation(Name,Email,Gender,Birthday); 
      userInformationsList.add(ID); 
      list(); 
      Toast.makeText(SearchActivity.this,Name,Toast.LENGTH_SHORT).show(); 
     } 
     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      Toast.makeText(SearchActivity.this,"Error 404",Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

private void list(){ 
    customAdapter customAdapterIntent= new customAdapter(this,R.layout.userslist,userInformationsList); 
    mListView.setAdapter(customAdapterIntent); 
} 
} 

UserInformation.java

public class UserInformation { 
     private static String Name; 
     private static String Gender; 
     private static String Email; 
     private static String Birthday; 

     public UserInformation(String Name,String Gender,String Email,String Birthday){ 
      this.Birthday=Birthday; 
      this.Email=Email; 
      this.Gender=Gender; 
      this.Name=Name; 
     } 

     public static String getName(){return Name;} 
     public static String getEmail(){return Email;} 
     public static String getGender(){return Gender;} 
     public static String getBirthday(){return Birthday;} 


} 

customAdapter.java

public class customAdapter extends ArrayAdapter<UserInformation> { 




    public customAdapter(Context context, int layoutResource, List<UserInformation> userInformationsList) { 
    super(context, layoutResource, userInformationsList); 

} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View view = convertView; 

    if (view == null) { 
     LayoutInflater layoutInflater = LayoutInflater.from(getContext()); 
     view = layoutInflater.inflate(R.layout.userslist, null); 
    } 

    UserInformation userInformation = getItem(position); 

    if (userInformation != null) { 
     TextView Name = (TextView) view.findViewById(R.id.textNameList); 
     //name.setText(name[position]); 
     TextView email = (TextView) view.findViewById(R.id.textEmailList); 
     //email.setText(Email[position]); 
     TextView gender = (TextView) view.findViewById(R.id.textGenderList); 
     //gender.setText(Gender[position]); 
     TextView birthday = (TextView) view.findViewById(R.id.textBirthdayList); 
     //birthday.setText(Birthday[position]); 

     if (Name != null) { 
      Name.setText(UserInformation.getName()); 
     } 
     if (email != null) { 
      email.setText(UserInformation.getEmail()); 
     } 
     if (gender != null) { 
      gender.setText(UserInformation.getGender()); 
     } 
     if (birthday != null) { 
      birthday.setText(UserInformation.getBirthday()); 
     } 
    } 

    return view; 
} 
} 

Resultado

+0

値。あなたはあなたのリストのデータを設定する位置にし、それは正常に動作します。 – Umair

+0

メソッド 'getItem()'を実装しましたか?もしそうなら、あなたはそれを投稿できますか? –

+0

@ Umairどういう意味なのか分かりません。 'setAdapter()'を 'onDataChange' voidに入れる必要がありますか? –

答えて

2

g値なぜクラス名の代わりにクラス(UserInformation)を使用していて、そのオブジェクトuserInformationを使用していますか?

if (Name != null) { 
     Name.setText(userInformation.getName()); 
    } 
    if (email != null) { 
     email.setText(userInformation.getEmail()); 
    } 
    if (gender != null) { 
     gender.setText(userInformation.getGender()); 
    } 
    if (birthday != null) { 
     birthday.setText(userInformation.getBirthday()); 
    } 

もう一つは、下記作ることは、あなたが間違ったアプローチを使用してリストビューにデータを追加している非静的

public class UserInformation { 
    private String Name; 
    private String Gender; 
    private String Email; 
    private String Birthday; 

    public UserInformation(String Name,String Gender,String Email,String Birthday){ 
     this.Birthday=Birthday; 
     this.Email=Email; 
     this.Gender=Gender; 
     this.Name=Name; 
    } 

    public String getName(){return Name;} 
    public String getEmail(){return Email;} 
    public String getGender(){return Gender;} 
    public String getBirthday(){return Birthday;} 

}

+0

それはうまく動作します、ありがとうございます –

+0

あなたは大歓迎です!私たちはお互いを助けるためにここにいる!しかし、あなたが私の答えを受け入れるなら、それは素晴らしいことだろう。ありがとう – Danger

関連する問題