我有来自实时数据库的数据,并且它已经绑定到FirebaseRecyclerAdapter
上,到现在为止,数据是按照将数据插入实时数据库中时的排序方式来获取的,但经过我排序之后它按类别或搜索顺序变得分散。
我添加了一个称为优先级的字段,从中我要分配要显示的第一项,依此类推。我该怎么做,我需要控制用户看到商品的顺序。
这是实时数据库及其所有字段的图像。我希望它按孩子的顺序排列-优先。
我还添加了以下代码,该代码用于绑定到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);
}
}
答案 0 :(得分:1)
要解决此问题,您应该使用如下查询:
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Data");
Query mQuery = mRef.orderByChild("prority");
如果您要查询特定的优先级,比如说优先级1
,请使用以下查询:
Query mQuery = mRef.orderByChild("prority").equalsTo(1);