gpt4 book ai didi

android - 未从 firebase 填充的 fragment 内的 ListView ,但我可以将数据保存到可能的 firebase DB

转载 作者:行者123 更新时间:2023-11-30 00:58:26 25 4
gpt4 key购买 nike

我有一个 MainActivity.Java 类,它使用 View 寻呼机来呈现我的 fragment 。其中一个 fragment 称为 DashBoardFrag,它应该从我的 firebase 数据库中选取数据并将其填充到 ListView 中。目前我可以发送数据并将其保存到 firebas 但我无法检索。检索应该在 DashBoardFrag 加载或显示给用户时发生。当我运行我的应用程序时,它不会崩溃,但再次没有显示任何内容。这是我在 DashBoardFrag fragment 中的 onCreateView 方法

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the fragment_dash_board for this fragment
View rootView = inflater.inflate(R.layout.fragment_dash_board, container, false);
newsListView = (ListView) rootView.findViewById(R.id.newslist_view);

dbref = FirebaseDatabase.getInstance().getReference();
dbOperationsHelper = new DBOperationsHelper(dbref);

/**
* SET ADAPTER
*/

//dbAdapter = new DBAdapter(getActivity().getApplicationContext(),dbOperationsHelper.retrieveNews());
arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,dbOperationsHelper.retrieveNews());

//Toast.makeText(getActivity().getApplicationContext(), "MUST NOT BE EMPTY", Toast.LENGTH_SHORT).show();
newsListView.setAdapter(dbAdapter);


return rootView;

}

这是我的 DBOperationsHelper

public class DBOperationsHelper {
DatabaseReference dbref;
Boolean saved;
ArrayList<String> newsArraylist = new ArrayList<>();

public DBOperationsHelper(DatabaseReference db){
this.dbref = db;
}

public Boolean save(DBModel news){
if(news == null){
saved = false;
}else{
try {
dbref.child("DBModel").push().setValue(news);
saved = true;
} catch (DatabaseException e) {

e.printStackTrace();
saved = false;
}
}
return saved;
}

private void fetchData(DataSnapshot dataSnapshot){
newsArraylist.clear();
for(DataSnapshot ds : dataSnapshot.getChildren()){
String news = ds.getValue(DBModel.class).getNewsHead();
newsArraylist.add(news);
}
}

/**
* RETRIEVE NEWS
*/
public ArrayList<String> retrieveNews(){
dbref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
return newsArraylist;
}
}

这是我的 DBModel 类

public class DBModel {
boolean bool;
String name, email, username, pass, newsHead, newsBody;

public void setIsLogin(boolean bl){
this.bool = bl;

}
public boolean getIsLogin(){
return this.bool;
}

public void setEmail(String email){
this.email = email;

}
public String getEmail(){
return this.email;
}

public void setUsername(String username){
this.username = username;

}
public String getUsername(){
return this.username;
}

public void setPass(String pass){
this.pass = pass;

}
public String getPass(){
return this.pass;
}

public void setNewsHead(String nws_name){
this.newsHead = nws_name;

}

public String getNewsHead() {
return newsHead;
}

public void setNewsBody(String newsBody) {
this.newsBody = newsBody;
}

public String getNewsBody() {
return newsBody;
}
}

最佳答案

最后我设法使用 FirebaseRecyclerAdapter 来填充我的回收器(从使用 ListView 更改为 recyclerview)。

enter image description here

这是我做的。

1.更改我的 DBModel(NewsModel) 对象类,使其具有 getterssetters 用于我想要显示的详细信息和如图所示检索

   public class NewsModel {
String email, username, pass, newsHead, newsBody;


public NewsModel() {
}

public NewsModel(String newsHead, String newsBody) {
this.newsHead = newsHead;
this.newsBody = newsBody;

}

public String getNewsHead() {
return newsHead;
}

public void setNewsHead(String nws_name){
this.newsHead = nws_name;

}

public String getNewsBody() {
return newsBody;
}

public void setNewsBody(String newsBody) {
this.newsBody = newsBody;
}


}

2.通过创建一个扩展 RecyclerViewHolder 的内部静态类来编辑我的 fragment ,我用它来设置文本字段的值,如图所示

DatabaseReference dbref;
FirebaseRecyclerAdapter<NewsModel,NewsModelVH> firebasenewsRecycleAdapter ;
RecyclerView newsrecyclerView;
LinearLayoutManager nwlinearLayoutManager;
ArrayList<NewsModel> newsArraylist = new ArrayList<>();



public static class NewsModelVH extends RecyclerView.ViewHolder{

public TextView newsHead, newsBody;

public NewsModelVH(View itemView) {
super(itemView);
this.newsHead = (TextView) itemView.findViewById(R.id.listview_item_title);
this.newsBody = (TextView) itemView.findViewById(R.id.listview_item_short_description);
}
}// End NewsModelVH class

public static final String NEWS= "NewsModel";

并编辑我的 onCreateView 方法以使用 FirebaseRecyclerAdapter,它使用 DBModel 类 DatabaseReferenceRecyccerViewHolder,如图所示

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the fragment_dash_board for this fragment
View rootView = inflater.inflate(R.layout.fragment_news_rv, container, false);
newsrecyclerView =(RecyclerView) rootView.findViewById(R.id.rv_noticeboard);
nwlinearLayoutManager = new LinearLayoutManager(getActivity());
nwlinearLayoutManager.setStackFromEnd(true);

dbref = FirebaseDatabase.getInstance().getReference();
firebasenewsRecycleAdapter = new FirebaseRecyclerAdapter<NewsModel, NewsModelVH>(
NewsModel.class,
R.layout.fragment_dashboard_imagetext,
NewsModelVH.class,
dbref.child(NEWS)) {
@Override
protected void populateViewHolder(NewsModelVH viewHolder, NewsModel model, int position) {
viewHolder.newsHead.setText(model.getNewsHead());
viewHolder.newsBody.setText(model.getNewsBody());
}
};

firebasenewsRecycleAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver(){
@Override
public void onItemRangeInserted(int positionStart, int itemCount){
super.onItemRangeInserted(positionStart, itemCount);
int newsCount = firebasenewsRecycleAdapter.getItemCount();
int lastVisiblePosition = nwlinearLayoutManager.findLastVisibleItemPosition();
if(lastVisiblePosition == -1 || (positionStart>= (newsCount -1) && lastVisiblePosition == (positionStart -1))){
newsrecyclerView.scrollToPosition(positionStart);
}
}
});
newsrecyclerView.setLayoutManager(nwlinearLayoutManager);
newsrecyclerView.setAdapter(firebasenewsRecycleAdapter);

/**
* SET ADAPTER
*/

ProgressBar newsprogressBar = (ProgressBar) rootView.findViewById(R.id.progress_bar);
newsprogressBar.setVisibility(View.VISIBLE);

Log.v("RETRIEVE", " dbOperationsHelper.retrieveNews() NEWS=" + dbref);

return rootView;

}

仍然让我头疼的一件事是为什么要显示数据,而我既没有使用 dbref.addValueEventListener(new ValueEventListener() 也没有使用 ref.addChildEventListener(new ChildEventListener()

好吧,有些事情告诉我 future 可能会限制我实现某些事情......目前还不确定是什么,但我会在那个时候知道。但是如果有人对此有解释也可以分享信息。

否则,希望这对某人有所帮助,这样您就不会在适配器之间切换两天(ArrayAdapterArrayListCustomeAdapter , FirebaseListAdapterFirebaseRecyclerAdapter) 来寻找解决方案。

关于android - 未从 firebase 填充的 fragment 内的 ListView ,但我可以将数据保存到可能的 firebase DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39806252/

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