我正在使用Firebase在Android Studio中构建一个简单的社交网络应用程序(我的第一个应用程序,所以对我来说很容易)。现在我正在开展一项活动,该活动将列出所有具有个人资料照片的用户以及每个用户的一些基本个人资料信息。某些用户拥有本地默认配置文件pic,而有些用户则从Firebase存储中获取。由于从存储中获取图片需要更长的时间,因此上传个人资料照片的用户的所有列表都会显示在列表的最后。如何防止这种情况发生和/或对列表进行排序?我还想在列表的末尾创建一个填充文本视图,但我还没有想出一个很好的方法。
以下是搜索活动的代码:
public class Search extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = database.getReference();
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final int numChildren = (int) dataSnapshot.getChildrenCount();
final Counter counter = new Counter();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
final User user = snapshot.getValue(User.class);
final ImageView profilePic = new ImageView(Search.this);
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.search_image), getResources().getDimensionPixelSize(R.dimen.search_image));
profilePic.setLayoutParams(imageParams);
if (user.profilePic.isEmpty()) {
LinearLayout layout2 = new LinearLayout(Search.this);
layout2.setPadding((int) getResources().getDimension(R.dimen.padding), (int) getResources().getDimension(R.dimen.padding), (int) getResources().getDimension(R.dimen.padding), (int) getResources().getDimension(R.dimen.empty));
layout2.setOrientation(HORIZONTAL);
layout2.setMinimumWidth(MATCH_PARENT);
layout.addView(layout2);
profilePic.setImageResource(R.mipmap.ic_launcher);
layout2.addView(profilePic);
LinearLayout layout3 = new LinearLayout(Search.this);
layout3.setOrientation(VERTICAL);
layout3.setMinimumHeight(WRAP_CONTENT);
layout3.setMinimumWidth(WRAP_CONTENT);
layout2.addView(layout3);
TextView un = new TextView(Search.this);
un.setText(user.username);
un.setTextSize(20);
un.setTypeface(Typeface.DEFAULT_BOLD);
un.setPadding((int) getResources().getDimension(R.dimen.padding),0,0,0);
layout3.addView(un);
TextView birthYear = new TextView(Search.this);
birthYear.setText("Birth Year: " + user.birthYear);
birthYear.setTextSize(18);
birthYear.setPadding((int) getResources().getDimension(R.dimen.padding),0,0,0);
layout3.addView(birthYear);
TextView gender = new TextView(Search.this);
gender.setText("Gender: " + user.gender);
gender.setTextSize(18);
gender.setPadding((int) getResources().getDimension(R.dimen.padding),0,0,0);
layout3.addView(gender);
TextView location = new TextView(Search.this);
location.setText("Location: " + user.location);
location.setTextSize(18);
location.setPadding((int) getResources().getDimension(R.dimen.padding),0,0,0);
layout3.addView(location);
counter.increment();
} else {
StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();
try {
final File localFile = File.createTempFile("profilePic", "jpg");
StorageReference ref = mStorageRef.child("images/" + snapshot.getKey() + "ProfilePic.jpg");
ref.getFile(localFile)
.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
LinearLayout layout2 = new LinearLayout(Search.this);
layout2.setPadding((int) getResources().getDimension(R.dimen.padding), (int) getResources().getDimension(R.dimen.padding), (int) getResources().getDimension(R.dimen.padding), (int) getResources().getDimension(R.dimen.empty));
layout2.setOrientation(HORIZONTAL);
layout2.setMinimumWidth(MATCH_PARENT);
layout.addView(layout2);
Bitmap bMap = BitmapFactory.decodeFile(localFile.toString());
profilePic.setImageBitmap(bMap);
layout2.addView(profilePic);
LinearLayout layout3 = new LinearLayout(Search.this);
layout3.setOrientation(VERTICAL);
layout3.setMinimumHeight(WRAP_CONTENT);
layout3.setMinimumWidth(WRAP_CONTENT);
layout2.addView(layout3);
TextView un = new TextView(Search.this);
un.setText(user.username);
un.setTextSize(20);
un.setTypeface(Typeface.DEFAULT_BOLD);
un.setPadding((int) getResources().getDimension(R.dimen.padding),0,0,0);
layout3.addView(un);
TextView birthYear = new TextView(Search.this);
birthYear.setText("Birth Year: " + user.birthYear);
birthYear.setTextSize(18);
birthYear.setPadding((int) getResources().getDimension(R.dimen.padding),0,0,0);
layout3.addView(birthYear);
TextView gender = new TextView(Search.this);
gender.setText("Gender: " + user.gender);
gender.setTextSize(18);
gender.setPadding((int) getResources().getDimension(R.dimen.padding),0,0,0);
layout3.addView(gender);
TextView location = new TextView(Search.this);
location.setText("Location: " + user.location);
location.setTextSize(18);
location.setPadding((int) getResources().getDimension(R.dimen.padding),0,0,0);
layout3.addView(location);
counter.increment();
if (counter.getCount() == numChildren) {
TextView padding = new TextView(Search.this);
padding.setPadding((int) getResources().getDimension(R.dimen.padding),(int) getResources().getDimension(R.dimen.padding),(int) getResources().getDimension(R.dimen.padding),(int) getResources().getDimension(R.dimen.padding));
layout.addView(padding);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(Search.this, "Error downloading image.",
Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
Toast.makeText(Search.this, "Error creating file.",
Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
谢谢!
答案 0 :(得分:0)
Firebase存储中目前没有API调用来列出您在帖子中提到的文件。如果您需要这样的功能,则应将文件的元数据(例如下载URL)存储在可以列出它们的位置。因此,您可以在Firebase Database的帮助下实现这一目标。拥有所有这些网址,您就可以按照自己想要的方式列出文件。
希望它有所帮助。