如何从Firebase缓存JSON数据以脱机使用? [重复]

我拥有成功在回收器视图中显示的json数据,现在我想使其脱机。我的回收器视图显示列表和onclick列表打开一个新的子活动以显示详细信息并具有imagesslider,因此我想使其在离线状态下均可用。我的代码如下

这是我的主要活动页面: 公开课

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_picnic);
    reference= FirebaseDatabase.getInstance().getReference().child("picnic");
    recyclerView=(RecyclerView)findViewById(R.id.recyclerpicniclist2);
    search=findViewById(R.id.picnic_search2);

    search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            adapter.getFilter().filter(s);
            return true;
        }
    });

    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    picniclist=new ArrayList<newpicnic>();
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
                newpicnic n=dataSnapshot1.getValue(newpicnic.class);
                picniclist.add(n);
            }
            adapter=new PicnicAdapter(picnic.this,picniclist);
            recyclerView.setAdapter(adapter);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(picnic.this,"Page is not found",Toast.LENGTH_SHORT).show();
        }
    });


}

}

这是我编写set和get方法的类:

public newpicnic() {
}

public String getSpotName() {
    return SpotName;
}

public void setSpotName(String spotName) {
    SpotName = spotName;
}

public String getDescription() {
    return Description;
}

public void setDescription(String description) {
    Description = description;
}

public String getAddress() {
    return Address;
}

public void setAddress(String address) {
    Address = address;
}

public List<String> getImg_url() {
    return img_url;
}

public void setImg_url(List<String> img_url) {
    this.img_url = img_url;
}

}

这是我的适配器:

public PicnicAdapter(Context c,ArrayList<newpicnic>n){
    context=c;
    picnic=n;
    picnic2=new ArrayList<>();
    picnic2.addAll(picnic);

}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater=LayoutInflater.from(context);
    View view=inflater.inflate(R.layout.activity_picnic_list_item,parent,false);

    //new holder for picnic details
    final MyViewHolder viewHolder=new MyViewHolder(view);
    viewHolder.view_picnic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent p = new Intent(context,picnic_details.class);
            p.putExtra("spot_name",picnic.get(viewHolder.getAdapterPosition()).getSpotName());
            p.putExtra("Description",picnic.get(viewHolder.getAdapterPosition()).getDescription());
            p.putExtra("Address",picnic.get(viewHolder.getAdapterPosition()).getAddress());
            context.startActivity(p);
        }
    });
    return viewHolder;

}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.SpotName.setText(picnic.get(position).getSpotName());

}

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

@Override
public Filter getFilter() {
    return  picnicDataFilter;
}
private Filter picnicDataFilter=new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        ArrayList<newpicnic> filteredList=new ArrayList<>();
        if (charSequence == null || charSequence.length()==0){
            filteredList.addAll(picnic2);
        }
        else {
            String filter=charSequence.toString().toLowerCase().trim();
            for(newpicnic dataItem:picnic2){
                if(dataItem.getSpotName().toLowerCase().contains(filter)){
                    filteredList.add(dataItem);
                }
            }
        }
        FilterResults results=new FilterResults();
        results.values=filteredList;
        return results;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
        picnic.clear();
        picnic.addAll((Collection<? extends newpicnic>) filterResults.values);
        notifyDataSetChanged();
    }
};

class MyViewHolder extends RecyclerView.ViewHolder{
    TextView SpotName;
    LinearLayout view_picnic;
    TextView Description;
    TextView Address;

    public MyViewHolder( View itemView) {
        super(itemView);
        SpotName=itemView.findViewById(R.id.SpotName);
        view_picnic=itemView.findViewById(R.id.list_root);
        Description=itemView.findViewById(R.id.picnic_description);
        Address=itemView.findViewById(R.id.picnic_address);

    }
}

}

这是我的子活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_picnic_details);




    Bundle r=getIntent().getExtras();
    pname=r.getString("spot_name");

    imgurl=new ArrayList<>();
    reference= FirebaseDatabase.getInstance().getReference("picnic");
    reference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot list:dataSnapshot.getChildren()){
                if (list.child("SpotName").getValue().toString().equals(pname)){
                    imgurl.addAll((List)list.child("img_url").getValue());
                }
            }
        }

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

        }
    });


    //Recieve data
    String placname2=getIntent().getExtras().getString("spot_name");
    String placename=getIntent().getExtras().getString("spot_name");
    String description=getIntent().getExtras().getString("Description");
    String address=getIntent().getExtras().getString("Address");


    //in views
    CollapsingToolbarLayout collapsingToolbarLayout=findViewById(R.id.collapsingbar);
    collapsingToolbarLayout.setTitleEnabled(true);

    TextView picnicplacename=findViewById(R.id.place_name2);
    TextView picnicdescription=findViewById(R.id.picnic_description);
    TextView picnicaddress=findViewById(R.id.picnic_address);
    //setting vlaues
    picnicdescription.setText(description);
    picnicaddress.setText(address);
    picnicplacename.setText(placname2);
    //img_url
    carouselView=findViewById(R.id.picnic_carousel);

    loadcarousel();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            loadcarousel();
        }
    },5000);






}
public void loadcarousel(){
    carouselView.setPageCount(imgurl.size());
     //setting image carousel
     carouselView.setImageListener(new ImageListener() {
         @Override
         public void setImageForPosition(int position, final ImageView imageView) {
             Glide.with(getApplicationContext()).asBitmap().load(imgurl.get(position)).into(new CustomTarget<Bitmap>() {
                 @Override
                 public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                     imageView.setImageBitmap(resource);
                 }

                 @Override
                 public void onLoadCleared(@Nullable Drawable placeholder) {

                 }
             });

    }
});
     //end of the slider

}

}

在此处输入代码

评论