当addValueEventListener调用

时间:2017-10-05 09:15:15

标签: java android listview firebase-realtime-database

我在Firebase上有一个数据库,并且正在尝试让android将其打印到ListView中。我可以让ListView打印一些东西,但它似乎是一些ID的值,而不是实际的值。关键是能够创建一个搜索栏,可以在数据库中找到任何数据。 我在addValueEventListener中有一些注释代码。这段代码应该可以使用,但是当我使用它时,当我转到" ExistingCustomerActivity"我的应用程序崩溃时没有错误。活动。 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");
}

2 个答案:

答案 0 :(得分:2)

您需要根据firebase数据库结构更改Buyers类变量或使用Map获取值

尝试以下代码,使用Buyers

中的地图获取值
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")
    }
}

答案 1 :(得分:0)

你必须这样做。

    mDatabaseReference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        //Buyers buyerList = new Buyers(childSnapshot);

                        for (DataSnapshot childSnapshot: dataSnapshot.getChildren())
                        {
                            Buyers buyerList = childSnapshot.getValue(Buyers.class);
                            //buyerList = childSnapshot.child("PersonNumberTextField").getValue(Buyers.class);
                            //Buyers buyerList = childSnapshot.child("PersonNumberTextField").getValue(Buyers.class);
                            //Buyers buyerList = childSnapshot.getValue(Buyers.class);
                            mUsernames.add(buyerList);
                        }
                        arrayAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });