【发布时间】: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