如何在Firebase Android的recyclerview / cardlayout中的顶部而不是底部显示最新帖子?

我对Java和Firebase完全陌生,在一个大学项目中工作.制作类似电子商务应用程序的内容,但实际上不是(类似).

到目前为止,默认情况下,新插入的数据位于底部,我需要将其置于顶部.在*上尝试了所有可能的答案. youtube和google结果-是徒劳的.老实说,我完全是一个入门者,不知道在哪里以及如何将它们集成在一起.

我正在使用的代码已发布此问题.提前致谢.
抱歉,我没有日志,因为我什至都不知道如何将代码(已经回答过)放在一起,请把它们放在这里.

我的“ Project.java”文件

public class Project {

String ctitle, cdetail, cbudget;



public Project() {

}

public String getCtitle() {
    return ctitle;
}

public void setCtitle(String ctitle) {
    this.ctitle = ctitle;
}

public String getCdetail() {
    return cdetail;
}

public void setCdetail(String cdetail) {
    this.cdetail = cdetail;
}

public String getCbudget() {
    return cbudget;
}

public void setCbudget(String cbudget) {
    this.cbudget = cbudget;
}

}

“ Adapter.java”文件

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
Context c;
ArrayList<Project> projects;

public MyAdapter(Context c, ArrayList<Project> projects) {
    this.c = c;
    this.projects = projects;
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(c).inflate(R.layout.model,parent,false);

    return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    final  Project s = projects.get(position);

    holder.titleTxt.setText(s.getCtitle());
    holder.detailTxt.setText(s.getCdetail());
    holder.budgetTxt.setText(s.getCbudget());


    holder.setItemClickListener(new ItemClickListener() {

        @Override
        public void onItemClick(int pos) {


            //OPEN DETAIL ACTIVITY
            openDetailActivity(s.getCtitle(),s.getCdetail(),s.getCbudget());
        }
    });
}
@Override
public int getItemCount() {

    return projects.size();
}

//OPEN DETAIL ACTIVITY
private void openDetailActivity(String...details)
{
    Intent i=new Intent(c,DetailActivity.class);
    i.putExtra("TITLE_KEY",details[0]);
    i.putExtra("DETAIL_KEY",details[1]);
    i.putExtra("BUDGET_KEY",details[2]);




    c.startActivity(i);
}

}

“ MyViewHolder.java”文件

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

TextView titleTxt, detailTxt, budgetTxt, datestampTxt;

ItemClickListener itemClickListener;

public MyViewHolder(View itemView) {

    super(itemView);

    titleTxt = (TextView) itemView.findViewById(R.id.titleTxt);
    detailTxt = (TextView) itemView.findViewById(R.id.detailTxt);
    budgetTxt = (TextView) itemView.findViewById(R.id.budgetTxt);


    itemView.setOnClickListener(this);

}

public void setItemClickListener(ItemClickListener itemClickListener)
{
    this.itemClickListener=itemClickListener;
}


@Override
public void onClick(View view) {
    this.itemClickListener.onItemClick(this.getLayoutPosition());
}

}

“ MAINACTIVITY.java”

public class Dashboard extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

DatabaseReference db;
FirebaseHelper helper;
MyAdapter adapter;
RecyclerView rv;
EditText et_insert_title, et_insert_detail, et_insert_budget;
Button btn_insert_new_project, sortBtn;

TextView datestampTxt;

NavigationView navigationView = null;
Toolbar toolbar = null;

//defining firebaseauth object
private FirebaseAuth firebaseAuth;


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


    //initializing firebase auth object
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    if (user != null) {
        for (UserInfo profile : user.getProviderData()) {
            // Id of the provider (ex: google.com)
            String providerId = profile.getProviderId();

            // UID specific to the provider
            String uid = profile.getUid();

            // Name, email address, and profile photo Url
            String name = profile.getDisplayName();
            String email = profile.getEmail();
            Uri photoUrl = profile.getPhotoUrl();
        };
    }

    /*//ADDED***

    if (firebaseAuth.getCurrentUser() == null) {
        finish();
    }
    firebaseAuth = FirebaseAuth.getInstance();

    FirebaseUser user_for_email = firebaseAuth.getCurrentUser();*/

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


    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setTitle("Dashboard");

    //SETUP RECYCLER
    rv = (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));

    //INITIALIZE OUR FIREBASE DB
    db = FirebaseDatabase.getInstance().getReference();
    db.keepSynced(true);

    helper = new FirebaseHelper(db);


    //ADAPTER
    //MADE NEW CHANGE HERE FOR REFERSHING DATA WITHOUT DOING ANYTHING
    adapter = new MyAdapter(this, helper.retrieve());
    rv.setAdapter(adapter);

    db.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            rv.setAdapter(adapter);
            adapter.notifyDataSetChanged();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            displayInputDialog();
        }
    });

    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.setDrawerListener(toggle);
    toggle.syncState();

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


}

private void displayInputDialog() {

    final Dialog d=new Dialog(this);
    d.setTitle("Post New Project");
    d.setContentView(R.layout.activity_create_new);
    et_insert_title= (EditText) d.findViewById(R.id.et_insert_title);
    et_insert_detail= (EditText) d.findViewById(R.id.et_insert_detail);
    et_insert_budget= (EditText) d.findViewById(R.id.et_insert_budget);


    btn_insert_new_project= (Button) d.findViewById(R.id.btn_insert_new_project);



    //SAVE
    btn_insert_new_project.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            //GET DATA
            String title=et_insert_title.getText().toString();
            String detail=et_insert_detail.getText().toString();
            String budget=et_insert_budget.getText().toString();
            /*String timestamp = datestampTxt.getText().toString();
            Long longlongdate = Long.parseLong(timestamp);*/


            //SET DATA
            Project s=new Project();
            s.setCtitle(title);
            s.setCdetail(detail);
            s.setCbudget(budget);






            //SIMPLE VALIDATION
            if(title != null && title.length()>0 && detail !=null && detail.length()>0 && budget != null && budget.length()>0)
            {
                //THEN SAVE
                if(helper.save(s))
                {
                    //IF SAVED CLEAR EDITTEXT AREA
                    et_insert_title.setText("");
                    et_insert_detail.setText("");
                    et_insert_budget.setText("");


                    adapter=new MyAdapter(Dashboard.this,helper.retrieve());
                    rv.setAdapter(adapter);


                    Toast.makeText(Dashboard.this, "Project Posted Successfully!", Toast.LENGTH_SHORT).show();


                }

            }


            else
            {
                Toast.makeText(Dashboard.this, "Please fill Everything!", Toast.LENGTH_SHORT).show();
            }

        d.dismiss();
        }


    });

    d.show();

}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

}

附言

更新:使用此方法使其工作.

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    linearLayoutManager.setStackFromEnd(true);
    linearLayoutManager.setReverseLayout(true);

解决方法:

试试这个希望,它将对您有帮助.

query.orderByChild("date").startAt(new DateTime().getMillis());

要么

Collections.reverse(arrayList);在传递给适配器之前将其反转.

上一篇:复杂的排序,可以通过cmp函数轻松完成,但是如何计划Python 3?


下一篇:javascript-按字母顺序对嵌套的div排序