获得价值vikas的标题后,如何根据其评级对Firebase数据进行排序?

时间:2018-07-27 04:47:41

标签: java android firebase firebase-realtime-database

意味着我要对标题为“ vikas”的数据进行查询后,首先按照最高的评级对firebaserecycleradapter进行排序

这是我的代码:

package com.onlinetuto;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
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;
import com.onlinetuto.model.blog;

import static com.onlinetuto.EmptyActivity.EXTRA_POST_KEY;

public class GetLocation extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private RecyclerView re;
    private DatabaseReference d,dref;
    private FirebaseAuth mauth;
    FirebaseRecyclerAdapter<blog,BlogViewholder> firebaseRecyclerAdapter;
    DatabaseReference  users, comments;
    ProgressBar progressBar;
    String locname;
    SharedPreferences sharedPreferences;
    LinearLayoutManager linearLayoutManager;
    AutoCompleteTextView autoCompleteTextView;
    Intent intent;
    Button button;
    Long code;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_location);
        intent=getIntent();
       if(intent!=null)
       {
           code= intent.getLongExtra("queryname",110094);
       }
        Toast.makeText(this, code.toString(), Toast.LENGTH_SHORT).show();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mauth = FirebaseAuth.getInstance();


        d = FirebaseDatabase.getInstance().getReference().child("Products");

        users = FirebaseDatabase.getInstance().getReference().child("Users");
        comments = FirebaseDatabase.getInstance().getReference().child("post-comments");
        Query nm=d.orderByChild("pincode").equalTo(code);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        progressBar.setVisibility(View.VISIBLE);
//        mquery = dtop.orderByChild("title").equalTo("hul");
//        mquery2 = dtop.orderByChild("likeCount").limitToFirst(100);
        //for offline capabilities of database
//        d3.keepSynced(true);
        d.keepSynced(true);
//        mdatabaselike.keepSynced(true);
        re = (RecyclerView) findViewById(R.id.blog_list);
        re.setHasFixedSize(true);
        linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);
        re.setLayoutManager(linearLayoutManager);


        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<blog, BlogViewholder>(
                blog.class,
                R.layout.blog_row,
                BlogViewholder.class,
                nm
        ) {
            @Override
            protected void populateViewHolder(final BlogViewholder viewHolder, final blog model, int position) {

                final String post_key = getRef(position).getKey();
                //for retrieving each post key getRef() method is used for this.
                final DatabaseReference post_ref = getRef(position);
                viewHolder.setTitle(model.getClassname());
                viewHolder.setImage(getApplicationContext(), model.getImageone());
                viewHolder.setReviews(model.getReviews());
                viewHolder.setTypo(model.getType());
                viewHolder.setNoofComments(model.getNoofreviews());
                viewHolder.setratings(model.getNetrat());
                viewHolder.setTextRating(model.getNoofreviews());
                viewHolder.vi.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent newIntent = new Intent(GetLocation.this, EmptyActivity.class);
                        newIntent.putExtra(EXTRA_POST_KEY, post_key);
                        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                        newIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(newIntent);
                    }
                });

              progressBar.setVisibility(View.GONE);
            }

        };


        re.setAdapter(firebaseRecyclerAdapter);

    }
    private Boolean exit = false;
    @Override
    public void onBackPressed() {
        if (exit) {
            finish(); // finish activity
        } else {
            Toast toast=Toast.makeText(this, "Press Back again to exit", Toast.LENGTH_SHORT);
            View view = toast.getView();
            view.setBackgroundColor(getResources().getColor(R.color.toa));
            TextView text = (TextView) view.findViewById(android.R.id.message);
            text.setTextColor(getResources().getColor(R.color.blackm));
            /*Here you can do anything with above textview like text.setTextColor(Color.parseColor("#000000"));*/
            toast.show();
            exit = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    exit = false;
                }
            }, 2 * 1000);

        }

    }
    public static class BlogViewholder extends RecyclerView.ViewHolder {
        View vi;
        FirebaseAuth mauth;
        EditText e1;

        public BlogViewholder(View itemView) {
            super(itemView);
            vi = itemView;

            mauth = FirebaseAuth.getInstance();

        }

        /*  public void setImage(Context context,String image){
              ImageView imageView=(ImageView)vi.findViewById(R.id.post_iamge);
              Picasso.with(context).load(image).into(imageView);

          }*/
        public void setTitle(String classname) {
            TextView textView = (TextView) vi.findViewById(R.id.textViewTitle);
            textView.setText(classname);
        }
        public void setImage(Context context, String image){
            ImageView imageView=(ImageView)vi.findViewById(R.id.imageView);
            Glide.with(context).load(image).into(imageView);
        }
        public void setReviews(String reviews) {
            TextView textView = (TextView) vi.findViewById(R.id.reviews);
            textView.setText(reviews);
        }
        public void setTypo(String type) {
            TextView textView = (TextView) vi.findViewById(R.id.type);
            textView.setText(type);


        }
        public void setTextRating(long comme) {
            TextView textView = (TextView) vi.findViewById(R.id.textViewRating);
            textView.setText(comme + "");


        }
        public void setNoofComments(long comme) {
            TextView textView = (TextView) vi.findViewById(R.id.reviews);
            textView.setText(comme+" Reviews");


        }
        public void setratings(Long rat) {
            RatingBar textView = (RatingBar) vi.findViewById(R.id.img);
            Integer myNum=0;

            if(rat==null)
                myNum=0;
            else
            {
                Integer integer=(int)(long)rat;
                myNum=integer;
            }
            if(myNum>5)
            {
                myNum=myNum%5;
                textView.setRating(myNum);
            }
            else
            {
                textView.setRating(myNum);
            }


        }
    }



}

这是我的代码,请帮助我。我想根据查询的等级对查询数据进行排序,然后再将其传递给firebase recycleradapter。如何实现? 产品数据库参考一项关键数据: img1 img2 img3 img4

我在这里使用Firebase实时数据库

1 个答案:

答案 0 :(得分:0)

我正在使用此邮件按最新帖子对其进行排序。

svgo-loader