【发布时间】:2018-06-17 19:03:18
【问题描述】:
我有 2 个不同的应用程序,它们使用一个 Firebase 实时数据库。 第一个将一些数据输入数据库的应用程序和其他必须启用才能读取数据库数据的应用程序。
我正在使用 listview 来检索第二个应用程序上的数据, 但不知何故,数据不会显示,应用程序只能显示一个空白屏幕。 我已经在第一个应用程序(我用来写入数据的应用程序)上测试了代码,它运行良好(它显示了所有数据)。
没有错误,但第二个应用的列表视图中仍然没有任何内容。
这是数据库
这是我的代码
private ListView listViewDemografi;
private List<Demografi> demografiList;
DatabaseReference databaseDemografi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseDemografi = FirebaseDatabase.getInstance().getReference("data");
listViewDemografi = (ListView) findViewById(R.id.listViewDemografi);
demografiList = new ArrayList<Demografi>();
}
@Override
protected void onStart (){
super.onStart();
//attaching value event listener
databaseDemografi.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//clearing the previous artist list
demografiList.clear();
//iterating through all the nodes
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//getting data
Demografi demografi = postSnapshot.getValue(Demografi.class);
//adding data to the list
demografiList.add(demografi);
}
//creating adapter
DemografiList artistAdapter = new DemografiList(MainActivity.this, demografiList);
//attaching adapter to the listview
listViewDemografi.setAdapter(artistAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Demografi.java
public class Demografi {
String dataJk;
String dataUsia;
String dataEdu;
String dataJob;
String dataEmail;
public Demografi(){
}
public Demografi(String dataJk, String dataUsia, String dataEdu, String dataJob, String dataEmail){
this.dataJk = dataJk;
this.dataUsia = dataUsia;
this.dataEdu = dataEdu;
this.dataJob = dataJob;
this.dataEmail = dataEmail;
}
public String getDataJk() {
return dataJk;
}
public String getDataUsia() {
return dataUsia;
}
public String getDataEdu() {
return dataEdu;
}
public String getDataJob() {
return dataJob;
}
public String getDataEmail() {
return dataEmail;
}
}
DemografiList.java
private Activity context;
List<Demografi> demografi;
public DemografiList(Activity context, List<Demografi> demografi) {
super(context, R.layout.layout_demografi_list, demografi);
this.context = context;
this.demografi = demografi;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.layout_demografi_list, null, true);
TextView tvJk = (TextView) listViewItem.findViewById(R.id.textViewJk);
TextView tvUsia = (TextView) listViewItem.findViewById(R.id.textViewUsia);
TextView tvEmail = (TextView) listViewItem.findViewById(R.id.textViewEmail);
TextView tvEdu = (TextView) listViewItem.findViewById(R.id.textViewPend);
TextView tvJob = (TextView) listViewItem.findViewById(R.id.textViewJob);
Demografi artist = demografi.get(position);
tvJk.setText(artist.getDataJk());
tvUsia.setText(artist.getDataUsia());
tvEmail.setText(artist.getDataEmail());
tvEdu.setText(artist.getDataEdu());
tvJob.setText(artist.getDataJob());
return listViewItem;
}
【问题讨论】:
-
两个应用程序的登录名不同,因为数据库中的 isAuthenticated 标志可能会导致这种情况
-
这些应用程序中仍然没有登录功能,因为我想先测试它而无需任何身份验证。
-
但是数据库呢?
-
我只是将数据库数据添加到帖子中,请检查一下
标签: android firebase firebase-realtime-database