我正在尝试手动将微调器数据添加到firebase,但是当我尝试在微调器下拉列表中显示它时,我收到错误:
无法将java.lang.String类型的对象转换为com.appname.appname.ListItem类型。
这是我从firebase检索微调器数据并在下拉微调器中显示它的方法:
proffRefspin.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.e("Spinner Data", "Spinner data is changed!");
Iterable<DataSnapshot> temp = dataSnapshot.getChildren();
ArrayList<String> list = new ArrayList<>();
for (DataSnapshot lists : temp){
Log.d("ddd","Array List: "+lists.getValue().toString());
list.add(lists.getValue().toString());
textView.setText(lists.getValue().toString());
}
showDataInSpinner(list);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e("Error", "Failed to read user", error.toException());
}
});
//在微调器中显示数据
public void showDataInSpinner(ArrayList<String> data) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_item, data
); //Create the Adapter to set the data
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //Set the layout resource to create the drop down views.
catSpinner.findViewById(R.id.defaultCats);
catSpinner.setAdapter(adapter); //Set the data to your spinner
}
这是我的POJO(ListItem模型)
public class ListItem {
private static final String TAG = "ListItem";
@Exclude
String userid;
private String username,summary,status,details,profession;
private String imageurl;
public ListItem(){}
public ListItem(String username, String summary, String status, String details, String profession, String imageurl) {
this.username = username;
this.summary = summary;
this.status = status;
this.details = details;
this.profession = profession;
this.imageurl = imageurl;
//this.userid = userid;
}
public String getUserid() {
return userid;
}
public void setUserid(String id) {
this.userid = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageUrl) {
this.imageurl = imageUrl;
}
}
当我在firebase数据库中手动添加我的职业列表时,我收到了错误消息。但是当firebase db为空时,不会显示错误。我在这做错了什么?我希望能够从firebase显示微调器数据。
这是我试图检索数据的方式
myRef = mFirebaseDatabase.getReference("Users").child("Professions").child("profession");
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ListItem data = dataSnapshot.getValue(ListItem.class);
listItems.add(data);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
这是类别片段方法,用于显示来自firebase的职业:
private void getFirebaseData() {
myRef = mFirebaseDatabase.getReference("Users").child("Professions").child("profession");
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ProfessionsList data = dataSnapshot.getValue(ProfessionsList.class);
proffItems.add(data);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
但我收到错误:
Can't convert object of type java.lang.String to type
com.appname.appname.ListItem。
图片firebase数据结构的屏幕截图: Firebase Data screenshot
答案 0 :(得分:0)
从firebase获取数据
ArrayList<Category> categoryList = new ArrayList<>();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
categoryList.add(postSnapshot.getValue(Category.class));
}
loadDataInSpinner();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
在微调器适配器中加载数据
private void loadDataInSpinner(){
categoryArrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, categoryList);
spinnerPostCategory.setAdapter(categoryArrayAdapter);
spinnerPostCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Category category = (Category) adapterView.getSelectedItem();
spinnerSelectedId= category.getCategoryId();
spinnerSelectedName = category.getCategoryName();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
答案 1 :(得分:0)
试试这个:
ArrayList<String> list = new ArrayList<>();
proffRefspin.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String profession=datas.child("profession").getValue().toString();
list.add(profession);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e("Error", "Failed to read user", error.toException());
}
});
只添加您在数据库中手动添加的专业,这样您就可以在微调器中拥有一个专业列表,用户以后可以选择。您可能需要更改位置(proffRefspin
),具体取决于您添加professions
的位置
将您的数据库更改为:
Users
Professions
randomid
professionname: software engineer
randomid
professionname: data science
然后这样做:
myRef = mFirebaseDatabase.getReference("Users").child("Professions");
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String prof=dataSnapshot.child("professionname").getValue().toString();
proffItems.add(prof);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});