【问题标题】:Why fragment listview firebase data becomes blank after initiating through it again?为什么片段listview firebase数据在再次启动后变为空白?
【发布时间】:2018-08-01 07:36:18
【问题描述】:

你好,我有一个片段,然后是一个列表视图。它通常会在应用程序启动时加载列表视图数据,但是当我使用导航抽屉再次单击片段时,所有片段都会消失并因此留下空白页面。

我的列表视图确实显示了自己,但问题是当我旋转屏幕或再次启动片段时,列表视图消失了。前任。来自列表视图可见的 prntscr.com/ihx3fg。进入列表视图消失的 prntscr.com/ihx3th。

我能问一下为什么会这样吗?

我的 CustomAdapter 代码

public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<User1> user;
public CustomAdapter(Context c, ArrayList<User1> user) {
    this.c = c;
    this.user = user;
}

@Override
public int getCount() {
    return user.size();
}

@Override
public Object getItem(int pos) {
    return user.get(pos);
}

@Override
public long getItemId(int pos) {
    return pos;
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
    if(convertView==null){
        convertView= LayoutInflater.from(c).inflate(R.layout.model,viewGroup,false);
    }
    TextView nameTxt= (TextView) convertView.findViewById(R.id.nameTxt);
    TextView emailTxt= (TextView) convertView.findViewById(R.id.emailTxt);
    TextView dateTxt= (TextView) convertView.findViewById(R.id.dateTxt);
    final User1 user= (User1) this.getItem(position);
    nameTxt.setText(user.getName());
    emailTxt.setText(user.getEmail());
    dateTxt.setText(user.getDate());

    return convertView;
}

Firebasehelper 代码

public class Firebasehelper {
DatabaseReference db;
Boolean saved=null;
ArrayList<User1> users=new ArrayList<>();
public Firebasehelper(DatabaseReference db) {
    this.db = db;
}
public Boolean save(User1 user) {
    if (user == null) {
        saved = false;
    } else {
        try {
            db.child("Accounts").child("Transaction").push().setValue(user);
            saved = true;

        } catch (DatabaseException e) {
            e.printStackTrace();
            saved = false;
        }
    }

    return saved;
}
private void fetchData(DataSnapshot dataSnapshot)
{
    User1 user=dataSnapshot.getValue(User1.class);
    users.add(user);

}
public ArrayList<User1>retrieve(){
    db.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 users;

我的片段活动代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   final View view = inflater.inflate(R.layout.fragment_member2, container, false);
    lv = (ListView) view.findViewById(R.id.lv);

    db= FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users");
    helper=new Firebasehelper(db);

    adapter=new CustomAdapter(getActivity(),helper.retrieve());
    lv.setAdapter(adapter);



    // Inflate the layout for this fragment
    return view;



}

我的用户类

public User1() {
    this.name=name;
    this.email=email;
    this.date=date;
}
public String getName(){
    return name;
}
public void setName(String name){
    this.name = name;
}
public String getEmail(){
    return email;
}
public void setEmail(String email){
    this.email=email;
}
public String getDate(){
    return date;
}
public void setDate(String date){
    this.date = date;
}

真的很困惑问题出在哪里,因为应用程序运行完美,除了片段列表视图会在再次启动片段时消失或变为空白。

【问题讨论】:

  • 旋转显示器时也会出现这种情况吗?
  • 是的,当我旋转显示器时它也会变成空白
  • @Yamiess 检查上面的链接,它会解决你的问题
  • 让我看看。我的列表视图确实显示了自己,但问题是当我旋转屏幕或再次启动片段时,列表视图消失了。前任。来自prntscr.com/ihx3fg,其中列表视图可见。进入列表视图消失的prntscr.com/ihx3th。我尝试了上面链接上的代码。似乎我需要在我的用户类上创建另一个方法来启动监听?

标签: android listview firebase arraylist


【解决方案1】:

出现这种行为是因为每次调用 onStop() 方法时适配器都会断开连接。

要解决这个问题,需要在onStart()方法中重新设置适配器。

【讨论】:

  • 谢谢你的回答先生,我真的很抱歉这个问题,但是我在哪里可以看到我的 onstop 和 on start 方法来设置我的适配器??
  • 如果您的活动中还没有这些方法,则需要实现它们。在 Android Studio 中点击ALT + Insert,选择Override Methods 并添加这两种方法。
  • 不,还是一样,空白。当我继续旋转屏幕时,数据再次显示,但当我再次旋转时消失。 (即使没有 onStart 也会这样做
  • 好的,您连接适配器,但确保已填充。如果它是空的,对你没有帮助。
  • 它填充了prntscr.com/ihx3fg,但是当我再次旋转屏幕时它变成了这样prntscr.com/ihx3th
【解决方案2】:

通过添加子事件监听器解决了这个问题

例如。

db.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            lv.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }

【讨论】:

  • 点击你的答案旁边的复选标记,它会变成绿色,这样人们就可以知道这对你有帮助!没问题!
  • 我还做不到,说要等明天~
猜你喜欢
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 2014-02-06
  • 2017-07-06
  • 1970-01-01
  • 2021-07-24
相关资源
最近更新 更多