我试图在活动开始时显示listView,其中条目位于firebase中,但是活动没有显示任何内容,当我点击屏幕上的某个地方时onItemClickListener正常工作
我的问题是因为onDataChange仅适用于数据库中的更改
在布局中,可见性全部设置为true,因此它与可见性
无关public class ViewUploads extends AppCompatActivity{
//the listview
ListView listView;
//database reference to get uploads data
DatabaseReference mDatabaseReference;
//list to store uploads data
List<Upload> uploadList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_uploads);
uploadList = new ArrayList<>();
listView = (ListView) findViewById(R.id.listView);
//adding a clicklistener on listview
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//getting the upload
Upload upload = uploadList.get(i);
//Opening the upload file in browser using the upload url
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(upload.getUrl()));
startActivity(intent);
}
});
//getting the database reference
mDatabaseReference = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);
//retrieving upload data from firebase database
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
uploadList.add(upload);
}
String[] uploads = new String[uploadList.size()];
for (int i = 0; i < uploads.length; i++) {
uploads[i] = uploadList.get(i).getName();
}
//displaying it to list
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, uploads);
listView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
答案 0 :(得分:0)
只需在String data = upload.getData();
Upload upload = postSnapshot.getValue(Upload.class);
即可
这里是getData(); method表示firebase中节点的getter方法....只需用自己的方法替换getData