I am totally new to Java and Firebase, working in a college project. Making something like an ecommerce app but not literally (somewhat similar).
To the point, my newly inserted data are at bottom as default, I need to get them at the top. Tried every possible answers at stackoverflow. youtube and google results - in vain. To be honest, I'm totally a starter and have no knowledge where and how to integrate them together.
Codes i'm working are posted with this question. Thanks in advance. Sorry I have no logs because I don't even know how to put (already answered) code together, far post them here.
My "Project.java" file
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" file
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" file
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();
}
}
}
P.S.
Update: Used this method to make it work.
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setReverseLayout(true);
try this Hope It will help you.
query.orderByChild("date").startAt(new DateTime().getMillis());
or
Collections.reverse(arrayList);
reverse it before passing to adapter.
Try this, hope it helps
mLayoutManager = new LinearLayoutManager(MyActivity.this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(yourAdapter);
来源:https://stackoverflow.com/questions/45399633/how-to-display-latest-post-on-top-instead-of-bottom-in-recyclerview-cardlayout