我在Firebase上有一个数据库,正在尝试让android将其打印到ListView中。我可以让ListView打印一些内容,但似乎是值的某些ID,而不是实际值。关键是要能够创建一个搜索栏,它可以找到数据库中的任何数据。
我在addValueEventListener中有一些注释的代码。该代码应该可以工作,但是当我使用它时,转到“ExistingCustomerActivity” Activity 时,我的应用程序会崩溃而不会出现错误。 My database structure and the output from the app, with the current code.请注意,ListView当前会在“买家”下方显示值。
有谁可以帮助解决这个问题?提前致谢。
更新:我可以看到数据已正确加载到日志中,但是它仍然显示我假定的某种ID。
public class ExistingCustomerActivity extends AppCompatActivity {
private DatabaseReference mDatabaseReference;
private ListView mUserList;
private ArrayList<Buyers> mUsernames = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_existing_customer);
mDatabaseReference = FirebaseDatabase.getInstance().getReference("Buyers");
mUserList = (ListView) findViewById(R.id.UserListView);
final ArrayAdapter<Buyers> arrayAdapter = new ArrayAdapter<Buyers>(this, android.R.layout.simple_list_item_1, mUsernames);
mUserList.setAdapter(arrayAdapter);
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Buyers buyerList = new Buyers(childSnapshot);
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
Map<String, String> map = (Map<String, String>) childSnapshot.getValue();
Log.v("YourValue,","Map value is:" +map.toString());
Buyers buyerList = new Buyers(map);
mUsernames.add(buyerList);
}
arrayAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
买家分类:
public class Buyers {
private String car_Value;
private String interest;
private String driverLicenseNr;
private String name;
private String personNr;
private String phone_Number;
public Buyers(Map<String,String> map){
car_Value = map.get("Bil");
interest = map.get("Interresse");
driverLicenseNr = map.get("Kørekortnr");
name = map.get("Navn");
personNr = map.get("Personnr");
phone_Number = map.get("Telefonnr");
}
最佳答案
您需要根据Firebase数据库结构更改Buyers
类变量,或使用Map获取值
尝试下面的代码以使用Buyers
中的Map获取值
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Buyers buyerList = new Buyers(childSnapshot);
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
Map<String,String> map=(Map<String, String>) childSnapshot.getValue();
Buyers buyerList = new Buyers(map);
mUsernames.add(buyerList);
}
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
将您的构造方法从
DataSnapshot
更改为Map
并获取值并将其设置为本地Buyers
变量。public class Buyers {
private String car_Value;
private String interest;
private String driverLicenseNr;
private String name;
private String personNr;
private String phone_Number;
public Buyers(Map<String,String map){
car_Value = map.get("Bil");
interest = map.get("Interresse")
driverLicenseNr = map.get("Korekortnr")
name = map.get("Navn")
personNr = map.get("Personnr")
phone_Number =map.get("Telefonnr")
}
}