gpt4 book ai didi

android - 根据来自 Firebase 的优先级对数据进行排序并将其显示在 FirebaseRecyclerAdapter 中

转载 作者:太空狗 更新时间:2023-10-29 13:46:45 27 4
gpt4 key购买 nike

我有来自实时数据库的数据,它被绑定(bind)到 FirebaseRecyclerAdapter 截至目前,数据正在按照数据插入实时数据库时的排序方式获取,但是在我按类别排序或搜索后,订单变得分散。

我添加了一个名为 priority 的字段,我想从中分配要显示的第一个项目等等。我该怎么做,我需要控制用户看到项目的顺序。

这是实时数据库及其所有字段的图像。我希望它由 child 订购 - 优先。

enter image description here

我还添加了下面的代码,我用它绑定(bind)到 FirebaseRecyclerAdapter

package com.test.testbv;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;


public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;

private FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FirebaseAuth.getInstance().getCurrentUser();

if (FirebaseAuth.getInstance().getCurrentUser() != null) {
// User is signed in.
} else {
// No user is signed in.
redirectToLogin();
}

// Action Bar

ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Coupons List");

mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);

// Set Layout
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

// Send Query to Firebase Db
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Data");

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}


@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Model, ViewHolder>(
Model.class,
R.layout.row,
ViewHolder.class,
mRef
) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, Model model, int position) {
viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getImage());
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);

viewHolder.setOnClickListener(new ViewHolder.clickListener() {

@Override
public void onItemClick(View view, int position) {
// get Data from firebase at the position clicked
String mTitle = getItem(position).getTitle();
String mDesc = getItem(position).getDescription();
String mImage = getItem(position).getImage();

// Pass this data to new activity
Intent intent = new Intent(view.getContext(), PostDetailActivity.class);

intent.putExtra("image", mImage);
intent.putExtra("title", mTitle);
intent.putExtra("description", mDesc);
startActivity(intent);

}

@Override
public void onItemLongClick(View view, int position) {

}

});

return viewHolder;
}
};
// Set adapter to recycler view
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
}

最佳答案

要解决这个问题,您应该使用如下所示的查询:

mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Data");
Query mQuery = mRef.orderByChild("prority");

如果您想查询特定的优先级,比如说优先级 1,请使用以下查询:

Query mQuery = mRef.orderByChild("prority").equalsTo(1);

关于android - 根据来自 Firebase 的优先级对数据进行排序并将其显示在 FirebaseRecyclerAdapter 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52400543/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com