我使用过firebaase recyclerview,并使用json上传填充它。 我有在JSON文件中订购的项目,并希望以相同的顺序接收它们。但是,每次我在我的应用程序中混淆它。怎么解决这个问题?
public class NoticesFragment extends Fragment {
private RecyclerView mNoticesList;
private DatabaseReference mDatabase;
// private ProgressDialog vProgress;
private View rootview;
private FirebaseRecyclerAdapter<Notices,NoticeViewHolder> firebaseRecyclerAdapter;
public NoticesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_notices, container, false);
Toast.makeText(getActivity(),"Please Wait While Load , It May Take Some Time ...", Toast.LENGTH_SHORT).show();
mDatabase = FirebaseDatabase.getInstance().getReference().child("notice");
mNoticesList = (RecyclerView) rootview.findViewById(R.id.notices_list);
mNoticesList.setLayoutManager(new LinearLayoutManager( getActivity() ) );
mNoticesList.setHasFixedSize(true);
return rootview;
}
@Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Notices, NoticeViewHolder>(
Notices.class,
R.layout.list_view_row,
NoticeViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(NoticeViewHolder viewHolder, Notices model, int position) {
viewHolder.setRefertext(model.getRefer());
viewHolder.setDatetext(model.getDate());
viewHolder.setLink(model.getLink());
}
};
mNoticesList.setAdapter(firebaseRecyclerAdapter);
}
public static class NoticeViewHolder extends RecyclerView.ViewHolder{
final View mView;
private String link;
private Intent myIntent;
public NoticeViewHolder(View itemView) {
super(itemView);
mView = itemView;
mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
mView.getContext().startActivity(myIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(mView.getContext(),"No Activity found to handle the request ",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
public void setRefertext(String refertext){
TextView referText = (TextView) mView.findViewById(R.id.refer_text);
referText.setText(refertext);
}
public void setDatetext (String datetext){
TextView dateText = (TextView) mView.findViewById(R.id.date_text);
dateText.setText(datetext);
}
public void setLink (String link){
this.link = link;
}
}
}
我该怎么做才能将我上传到firebase的有序JSON?我想以相同的顺序取回物品。
答案 0 :(得分:1)
根据定义,JSON是无序的。因此,您上传的JSON文件中的项目存在的方式会丢失,因为它会持久保存到数据库中。
要订购数据,您在检索数据时使用Firebase的orderBy...
方法。例如,如果您的通知具有时间戳属性,则可以使用以下命令在Recycler视图中对其进行排序:
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Notices, NoticeViewHolder>(
Notices.class,
R.layout.list_view_row,
NoticeViewHolder.class,
mDatabase.orderByChild("timestamp")
) {
我建议您花一些时间在Firebase documentation,特别是section on ordering and filtering。