daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class UsersActivity extends AppCompatActivity {
  2.  
  3.     //Layout
  4.     private Toolbar mToolbar;
  5.     private RecyclerView mUserList;
  6.  
  7.     //Firebase
  8.     private DatabaseReference mUsersDatabase;
  9.  
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_users);
  14.  
  15.         //Layour
  16.         mToolbar = (Toolbar) findViewById(R.id.users_toolbar);
  17.         setSupportActionBar(mToolbar);
  18.         getSupportActionBar().setTitle("All Users");
  19.         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  20.         mUserList = (RecyclerView) findViewById(R.id.users_list);
  21. //        mUserList.setHasFixedSize(true);
  22.         mUserList.setLayoutManager(new LinearLayoutManager(this));
  23.  
  24.         //Firebase
  25.         mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
  26.     }
  27.  
  28.     @Override
  29.     protected void onStart() {
  30.         super.onStart();
  31.  
  32.         Log.v("=====> IN ON START", "in onstart");
  33.  
  34.         Query query = mUsersDatabase
  35.                 .limitToLast(20);
  36.  
  37.         FirebaseRecyclerOptions<Users> options =
  38.                 new FirebaseRecyclerOptions.Builder<Users>()
  39.                         .setQuery(query, Users.class)
  40.                         .build();
  41.  
  42.         //Pass model + viewholder
  43.         //Pass model / layout of item / ViewHolder / reference to Databace place
  44.         FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter
  45.                 <Users, UsersViewHolder>(options) {
  46.             @Override
  47.             protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {
  48.  
  49.                 String s = model.getName().toString();
  50.                 Log.v("=====> ", " " + s);
  51.                 holder.setName(model.getName());
  52.                 holder.setStatus(model.getStatus());
  53.                 holder.setImage(model.getImage());
  54.             }
  55.  
  56.             @Override
  57.             public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  58.                 View view = LayoutInflater.from(parent.getContext())
  59.                         .inflate(R.layout.user_item, parent, false);
  60.                 Log.v("=====> ", "Creating View Holder ");
  61.  
  62.                 return new UsersViewHolder(view);
  63.             }
  64.         };
  65.  
  66.         mUserList.setAdapter(firebaseRecyclerAdapter);
  67.         firebaseRecyclerAdapter.startListening();
  68.  
  69.     }
  70.  
  71.     public static class UsersViewHolder extends RecyclerView.ViewHolder {
  72.  
  73.         View view;
  74.         Context c;
  75.  
  76.         public UsersViewHolder(View itemView) {
  77.             super(itemView);
  78.             view = itemView;
  79.         }
  80.  
  81.         public void setName(String name) {
  82.  
  83.             TextView txtUserName = (TextView) view.findViewById(R.id.user_single_name);
  84.             txtUserName.setText(name);
  85.  
  86.  
  87.         }
  88.  
  89.         public void setStatus(String status) {
  90.             TextView txtstatus = (TextView) view.findViewById(R.id.user_single_status);
  91.             txtstatus.setText(status);
  92.         }
  93.  
  94.         public void setImage(final String image) {
  95.  
  96.             final CircleImageView img = (CircleImageView) view.findViewById(R.id.user_single_img);;
  97.  
  98.             //  Picasso.with(c).load(image).into(img);
  99.  
  100.             Picasso.with(c).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(img, new Callback() {
  101.                 @Override
  102.                 public void onSuccess() {
  103.                     // Offline Download
  104.                 }
  105.  
  106.                 @Override
  107.                 public void onError() {
  108.                     Picasso.with(c).load(image).into(img);
  109.                 }
  110.             });
  111.  
  112.         }
  113.  
  114.         public View getView() {
  115.             return view;
  116.  
  117.         }
  118.     }
  119. }
RAW Paste Data
Top