我想在recyclerview开始时显示上传到Firebase存储的最新图像

时间:2019-10-14 13:15:05

标签: java android html firebase firebase-realtime-database

我正在将图像上传到Firebase存储,然后在数据库中推送该图像的名称和URL。接下来,我将检索图像URL,以在我的recyclerview中显示图像。现在,我想在recyclerview开始时显示上载到Firebase存储的最新图像。如何在Firebase数据库中更改顺序?

现在,您添加的第一张图像是数据库中的第一张图像。我想要用户上传的最新图像,该图像的url应该位于firebase数据库的顶部。

每张图片

Name: tom
Desc: 1
Url: any

Name: miki
Desc: 2
Url: any

描述:1最旧

描述:2是最新的

如何更改订单,请给我完整的代码

WallpapersActivity

public class WallpapersActivity extends AppCompatActivity {

List<Wallpaper> wallpaperList;
List<Wallpaper> favList;
RecyclerView recyclerView;
WallpapersAdapter adapter;

DatabaseReference dbWallpapers, dbFavs;
ProgressBar progressBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wallpapers);


    Intent intent = getIntent();
    final String category = intent.getStringExtra("category");

    Toolbar toolbar = findViewById(R.id.toolbar);
    toolbar.setTitle(category);
    setSupportActionBar(toolbar);

    favList = new ArrayList<>();
    wallpaperList = new ArrayList<>();
    recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adapter = new WallpapersAdapter(this, wallpaperList);

    recyclerView.setAdapter(adapter);

    progressBar = findViewById(R.id.progressbar);



    dbWallpapers = FirebaseDatabase.getInstance().getReference("images")
            .child(category);



    if(FirebaseAuth.getInstance().getCurrentUser() != null){
        dbFavs = FirebaseDatabase.getInstance().getReference("users")
                .child( FirebaseAuth.getInstance().getCurrentUser().getUid() )
                .child( "favourites" )
                .child( category );

        fetchFavWallpapers( category );
    }else {
        fetchWallpapers( category );
    }
}

WallpapersAdapter

public class WallpapersAdapter extends RecyclerView.Adapter<WallpapersAdapter.WallpaperViewHolder> {

private Context mCtx;
private List<Wallpaper> wallpaperList;
DatabaseReference dbWallpapers;


public WallpapersAdapter(Context mCtx, List<Wallpaper> wallpaperList) {
    this.mCtx = mCtx;
    this.wallpaperList = wallpaperList;


}

@Override
public WallpaperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mCtx).inflate( R.layout.recyclerview_wallpapers, parent, false);
    return new WallpaperViewHolder(view);

}

@Override
public void onBindViewHolder(WallpaperViewHolder holder, int position) {
    Wallpaper w = wallpaperList.get(position);
    holder.textView.setText(w.title);
    Glide.with(mCtx)
            .load(w.url)
            .into(holder.imageView);

    if (w.isFavourite) {
        holder.checkBoxFav.setChecked(true);
    }
}

@Override
public int getItemCount() {
    return wallpaperList.size();
}


class WallpaperViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    TextView textView;
    ImageView imageView;
}

CategoriesAdapter

public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.CategoryViewHolder> {

private Context mCtx;
private List<Category> categoryList;

public CategoriesAdapter(Context mCtx, List<Category> categoryList) {
    this.mCtx = mCtx;
    this.categoryList = categoryList;
}

@Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_categories, parent, false);
    return new CategoryViewHolder(view);
}

@Override
public void onBindViewHolder(CategoryViewHolder holder, int position) {
    Category c = categoryList.get(position);
    holder.textView.setText(c.name);
    Glide.with(mCtx)
            .load(c.thumb)
            .into(holder.imageView);
}

@Override
public int getItemCount() {
    return categoryList.size();
}

class CategoryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView textView;
    ImageView imageView;

    public CategoryViewHolder(View itemView) {
        super(itemView);

        textView = itemView.findViewById(R.id.text_view_cat_name);
        imageView = itemView.findViewById(R.id.image_view);

        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int p = getAdapterPosition();
        Category c = categoryList.get(p);

        Intent intent = new Intent(mCtx, WallpapersActivity.class);
        intent.putExtra("category", c.name);

        mCtx.startActivity(intent);
    }
}

Firebase Database

Apps RecyclerView

2 个答案:

答案 0 :(得分:2)

添加此行,然后在“ WallpapersActivity”中设置适配器:

                Collections.reverse( wallpaperList);

答案 1 :(得分:0)

您可以在获取子值时使用时间戳记,并且可以在顶部显示最新的时间戳记。

UINavigationBar

然后您的安全性和规则中的内容类似于:

ref.child("myData").orderByChild("timestamp").limitToLast(10)

提高性能