gpt4 book ai didi

android - 如何在 firebase android 的 recyclerview/cardlayout 中将最新帖子显示在顶部而不是底部?

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:33 24 4
gpt4 key购买 nike

我是 Java 和 Firebase 的新手,在大学项目中工作。制作类似于电子商务应用程序的东西,但不是字面意思(有点相似)。

至此,我新插入的数据默认在底部,我需要将它们放在顶部。在 stackoverflow 上尝试了所有可能的答案。 youtube 和谷歌结果 - 徒劳无功。老实说,我完全是初学者,不知道在哪里以及如何将它们集成在一起。

我正在处理的代码与此问题一起发布。提前致谢。抱歉,我没有日志,因为我什至不知道如何将(已回答的)代码放在一起,请将它们张贴在这里。

我的“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;
}

“适配器.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);

最佳答案

试试看,希望对你有帮助

mLayoutManager = new LinearLayoutManager(MyActivity.this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);

mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(yourAdapter);

关于android - 如何在 firebase android 的 recyclerview/cardlayout 中将最新帖子显示在顶部而不是底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45399633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com