如何在没有互联网的情况下显示最后一个列表(firebase android)

时间:2019-04-02 17:43:13

标签: android performance android-layout

如何显示没有互联网的最新用户列表。

当我使用互联网时,数据显示成功

当我不使用互联网时,没有数据

我需要最后的数据才能显示在没有互联网的列表中

活动

Toolbar tb;
DatabaseReference onlineref;
RecyclerView recyclerview;
RecyclerView.LayoutManager layoutManager;
ArrayList<User>list;
AntherUS antherUS;
UserCOntroller adapter;
User p;
TextView title_Txt; 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_);

    antherUS=new AntherUS(list_Activity.this);
    title_Txt=(TextView)findViewById(R.id.title_text);


  //  DatabaseReference reference =FirebaseDatabase.getInstance().getReference("Users");
    //reference.addValueEventListener(new V)

    //toolbar
    tb=(Toolbar) findViewById(R.id.tb);
    setSupportActionBar(tb);
    title_Txt.setText("  "+antherUS.reading_code()+" ~List");
    getSupportActionBar().setTitle(antherUS.reading_code());
    int redColorValue = Color.WHITE;


    String smartcode=antherUS.reading_code();

    onlineref= FirebaseDatabase.getInstance().getReference("Users").child(smartcode+" ~List");
    recyclerview =(RecyclerView)findViewById(R.id.recy);
    layoutManager=new LinearLayoutManager(this);
    recyclerview.setLayoutManager(layoutManager);
    onlineref.keepSynced(true);

    list=new ArrayList<User>();
    adapter=new UserCOntroller(list_Activity.this,list);


        onlineref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (final DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){

                    if (!dataSnapshot1.getKey().equals(antherUS.reading_code()+"pw_Group")) {
                         p = dataSnapshot1.getValue(User.class);
                        String[] Email_part = antherUS.reading_email().split("@");
                        String part1 = Email_part[0];
                        String aa = dataSnapshot1.getKey();
                        FirebaseUser userse = FirebaseAuth.getInstance().getCurrentUser();
                        String as = antherUS.reading_name();
                        if (!aa.equals("chat") && (aa != null) && (!aa.equals(antherUS.reading_holename())) && (!aa.equals(antherUS.reading_name())) && (!aa.equals(userse.getUid()))
                                && (!aa.equals(part1))) {

                            list.add(p);
                            antherUS.writing_numberlist(list.size());
                        adapter.notifyDataSetChanged();
                        }}
                }

                recyclerview.setAdapter(adapter);


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });



    recyclerview.getRecycledViewPool().setMaxRecycledViews(0,list.size());
    recyclerview.setItemViewCacheSize(list.size());

}

@Override
protected void onResume() {
    super.onResume();
    User user=new User();
    user.setPw_group(antherUS.reading_pwgroup());
    user.setCode(antherUS.reading_code());
    user.setEmail(antherUS.reading_email());
    user.setName(antherUS.reading_name());
    user.setHole(antherUS.reading_holename());

}

@Override
protected void onPause() {
    super.onPause();

}

@Override
public void onStart() {
    super.onStart();

}
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

}

RecyclerView适配器

public class UserCOntroller extends RecyclerView.Adapter<UserCOntroller.myViewHolder>{

Context context;
ArrayList<User>profiles;
String massemail;

public  UserCOntroller(Context c,ArrayList<User>p){
    context=c;
    profiles=p;
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    return new myViewHolder(LayoutInflater.from(context).inflate(R.layout.user_layout,viewGroup,false));
}

@Override
public void onBindViewHolder(@NonNull final myViewHolder myViewHolder, final int position) {
    AntherUS antherUS=new AntherUS(context);

    // set names in list
    myViewHolder.user.setText(profiles.get(position).getName());
    final User user=profiles.get(position);


    /// get url
    massemail=user.getEmail();
    Glide.with(context)
            .load(user.getImageUrl())
            .into(myViewHolder.imageView);

myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent=new Intent(context,CHatActivity.class);
       // User user=profiles.get(position);
        //send name and the main name
        intent.putExtra("userid",user.getName());
        intent.putExtra("holeid",user.getHole());
        FirebaseUser user1s=FirebaseAuth.getInstance().getCurrentUser();
        context.startActivity(intent);

    }
});

}

@Override
public int getItemCount() {
    return profiles.size();
}

class myViewHolder extends RecyclerView.ViewHolder{

    TextView user;
    CircleImageView imageView;
    CardView card;
    public myViewHolder(@NonNull View itemView) {
        super(itemView);

        user=(TextView)itemView.findViewById(R.id.username);
        imageView=(CircleImageView) itemView.findViewById(R.id.imageView);
        card=(CardView) itemView.findViewById(R.id.card);
        card.getBackground().setAlpha(0);

    }
}
}

2 个答案:

答案 0 :(得分:0)

您必须将列表数据存储在本地数据库中,并且当互联网不可用时,您应该从本地数据库中获取数据并显示给用户。

答案 1 :(得分:0)

Firebase提供了在本地缓存数据的选项,以便我们离线时可以使用它们。有关更多信息,请检查文档Enabling Offline Capabilities

要在本地存储数据,必须将setPersistenceEnabled设置为true

FirebaseDatabase.getInstance().setPersistenceEnabled(true);