Created
January 22, 2018 12:25
-
-
Save AnkitSuda/1eb9fbba2fc6848fe89b28efeee169ce to your computer and use it in GitHub Desktop.
FRA fetch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void fetch() { | |
| Query query = FirebaseDatabase.getInstance() | |
| .getReference() | |
| .child("posts"); | |
| FirebaseRecyclerOptions<Model> options = | |
| new FirebaseRecyclerOptions.Builder<Model>() | |
| .setQuery(query, new SnapshotParser<Model>() { | |
| @NonNull | |
| @Override | |
| public Model parseSnapshot(@NonNull DataSnapshot snapshot) { | |
| return new Model(snapshot.child("id").getValue().toString(), | |
| snapshot.child("title").getValue().toString(), | |
| snapshot.child("desc").getValue().toString()); | |
| } | |
| }) | |
| .build(); | |
| adapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) { | |
| @Override | |
| public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
| View view = LayoutInflater.from(parent.getContext()) | |
| .inflate(R.layout.list_item, parent, false); | |
| return new ViewHolder(view); | |
| } | |
| @Override | |
| protected void onBindViewHolder(ViewHolder holder, final int position, Model model) { | |
| holder.setTxtTitle(model.getmTitle()); | |
| holder.setTxtDesc(model.getmDesc()); | |
| holder.root.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| } | |
| }; | |
| recyclerView.setAdapter(adapter); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment