gpt4 book ai didi

android - ListView 适配器不能应用于 Modal 类

转载 作者:行者123 更新时间:2023-11-29 02:41:43 26 4
gpt4 key购买 nike

当我尝试为我的 listView 放置适配器时出现错误。它不能应用于我的模态类,即 UserInformation.java。我该如何解决这个问题?

错误:mlistView.setAdapter(adapter)

public class FindDonorActivity extends AppCompatActivity {


ListView mlistView;
DatabaseReference databaseReference;
List<UserInformation> userList;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_donor);

databaseReference = FirebaseDatabase.getInstance().getReference("user");

mlistView = (ListView) findViewById(R.id.mlistView);

userList = new ArrayList<>();
}

@Override
protected void onStart() {
super.onStart();

databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

userList.clear();

for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {

UserInformation user = userSnapshot.getValue(UserInformation.class);
userList.add(user);



}


UserInformation adapter = new UserInformation(FindDonorActivity.this, userList);
mlistView.setAdapter(adapter);

}



@Override
public void onCancelled(DatabaseError databaseError) {

}
});


}



}

用户信息.java

public class UserInformation {

public String useridnumber;
public String name;
public String email;
public String state;
public String bloodgroup;
public String lastdonated;

public UserInformation(FindDonorActivity findDonorActivity, List<UserInformation> userList){


}

public UserInformation (String useridnumber, String name, String email, String state, String bloodgroup, String lastdonated){

this.useridnumber = useridnumber;
this.name = name;
this.email = email;
this.state = state;
this.bloodgroup = bloodgroup;
this.lastdonated = lastdonated;

}

public String getUseridnumber() {

return useridnumber;
}



public String getBloodgroup() {
return bloodgroup;
}

public String getState() {
return state;
}

public String getLastdonated() {
return lastdonated;
}

public String getEmail() {
return email;
}
}

更新:这是我的适配器类

public class UserList extends ArrayAdapter<UserInformation> {

private Activity context;
private List<UserInformation> userList;

public UserList (Activity context, List<UserInformation> userList){
super(context, R.layout.list_layout, userList);
this.context = context;
this.userList = userList;

}

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

LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_layout, null, true);

TextView tvbloodlist = (TextView) listViewItem.findViewById(R.id.tvbloodlist);
TextView tvstatelist = (TextView) listViewItem.findViewById(R.id.tvstatelist);
TextView tvdonatedlist = (TextView) listViewItem.findViewById(R.id.tvdonatedlist);
TextView tvemaillist = (TextView) listViewItem.findViewById(R.id.tvemaillist);

UserInformation user = userList.get(position);

tvbloodlist.setText(user.getBloodgroup());
tvstatelist.setText(user.getState());
tvdonatedlist.setText(user.getLastdonated());
tvemaillist.setText(user.getEmail());

return listViewItem;
}

最佳答案

您需要使用一个ArrayAdapter 并将UserInformation 对象的ArrayList 传递给它。 UserInformation 对象不是 ArrayAdapter。这应该有帮助 - ArrayAdapter

编辑 - 您不应将 FindDonorActivity 的实例传递给 UserInformation 对象。您的 UserInformation 不应引用任何 Activity

关于android - ListView 适配器不能应用于 Modal 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43599712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com