Java 自定义阵列适配器仅显示最后的信息
我有一个Java 自定义阵列适配器仅显示最后的信息,java,android,list,listview,android-arrayadapter,Java,Android,List,Listview,Android Arrayadapter,我有一个ArrayAdapter,它列出了用户的数据,但它列出了最后一个用户的数据,它显示了正确的行数,如果有5个用户,它会显示5行,但它们都是相同的,最后一个用户 主要活动是: for(DataSnapshot Ds : dS.getChildren()) { Object key = Ds.getKey(); String StringKey = String.valueOf(key); getname(StringKey); } private void getname(String
ArrayAdapter
,它列出了用户的数据,但它列出了最后一个用户的数据,它显示了正确的行数,如果有5个用户,它会显示5行,但它们都是相同的,最后一个用户
主要活动是:
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;
}
}
公共类customAdapter扩展了ArrayAdapter{
公共customAdapter(上下文上下文、内部布局资源、列表用户信息列表){
超级(上下文、布局资源、用户信息列表);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=转换视图;
如果(视图==null){
LayoutInflater LayoutInflater=LayoutInflater.from(getContext());
视图=LayoutFlater.inflate(R.layout.userslist,null);
}
UserInformation UserInformation=getItem(位置);
if(userInformation!=null){
TextView Name=(TextView)view.findViewById(R.id.textNameList);
//name.setText(名称[职位]);
TextView email=(TextView)view.findViewById(R.id.textEmailList);
//email.setText(email[position]);
TextView性别=(TextView)view.findViewById(R.id.textGenderList);
//性别.setText(性别[职位]);
TextView生日=(TextView)view.findViewById(R.id.textBirthdayList);
//生日.setText(生日[职位]);
if(Name!=null){
Name.setText(UserInformation.getName());
}
如果(电子邮件!=null){
setText(UserInformation.getEmail());
}
if(性别!=null){
setText(UserInformation.getGender());
}
如果(生日!=null){
setText(UserInformation.getbirth());
}
}
返回视图;
}
}
要设置值,为什么要使用类(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;}
}您使用错误的方法在listview中添加数据。您可以在列表中设置数据,这样它就可以正常工作。您是否实现了方法
getItem()
?如果可以的话,你可以发布它吗?@Umair我不明白你的意思,我应该把setAdapter()
放在onDataChange
void中吗?@gil.fernandes不,我不知道getItem
用于什么?我应该把它放在哪里?@Enriqueramosumñoz不。我的意思是你没有正确设置列表中的数据。非常欢迎你!我们是来互相帮助的!但如果你能接受我的回答那就太好了。谢谢