【问题标题】:Custom ArrayAdapter display the last Information only自定义 ArrayAdapter 仅显示最后的信息
【发布时间】:2018-05-29 09:34:08
【问题描述】:

我有一个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);
}
}

用户信息.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;
}
}

【问题讨论】:

  • 您使用错误的方法在列表视图中添加数据。您可以在列表中设置数据,然后它会正常工作。
  • 你实现了getItem()这个方法吗?如果可以的话可以发一下吗?
  • @Umair 我不明白你的意思,我应该把setAdapter() 放在onDataChange void 中吗?
  • @gil.fernandes 不,我没有 getItemfor 是什么?我应该把它放在哪里?
  • @EnriqueRamosMuñoz 没有。我的意思是说您没有在列表中正确设置数据。

标签: java android list listview android-arrayadapter


【解决方案1】:

对于设置值,您为什么使用类 (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;}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多