adapter example by kherwara app

 package com.example.rscit.act;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.rscit.R;
import com.example.rscit.databinding.ActivityChapterDBinding;
import com.example.rscit.databinding.CdBinding;
import com.example.rscit.model.NotesData;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.Objects;

public class ChapterD extends AppCompatActivity {

private ActivityChapterDBinding binding;
private String receivedData;
private ArrayList<NotesData> topicList;
private ChapterListDAdapter chapterListDAdapter;

private int currentItemIndex = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityChapterDBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

receivedData = getIntent().getStringExtra("key");

if (receivedData != null) {
Objects.requireNonNull(getSupportActionBar()).setTitle(receivedData);

// Initialize RecyclerView
topicList = new ArrayList<>();
chapterListDAdapter = new ChapterListDAdapter(this, topicList);

LinearLayoutManager manager = new LinearLayoutManager(this);
binding.topicl.setAdapter(chapterListDAdapter);
binding.topicl.setLayoutManager(manager);

// Load data from Firebase
DatabaseReference databaseReference = FirebaseDatabase.getInstance()
.getReference(String.format("Rscit/Chapter/%s", receivedData));

databaseReference.addValueEventListener(new ValueEventListener() {
@SuppressLint("NotifyDataSetChanged")
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
topicList.clear(); // Clear the list before adding new data

for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
// Check if the data at this address is a valid NotesData object
if (snapshot.exists() && snapshot.hasChildren()) {
NotesData notesData = snapshot.getValue(NotesData.class);

if (notesData != null) {
topicList.add(notesData);
}
} else {
// Handle the case where the data is not in the expected format
// You may log a warning or take appropriate action
}
}

if (topicList.isEmpty()) {
// Handle the case where there is no valid NotesData available
Toast.makeText(ChapterD.this, "No valid data available", Toast.LENGTH_SHORT).show();
} else {
// Add the first item to the list and update the adapter
chapterListDAdapter.updateItem(topicList.get(0), 0);
}

binding.topicl.setVisibility(View.VISIBLE);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ChapterD.this, "Database Error: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});

}
}

private void showNextItem(View v) {
navigateItems();
}

private void navigateItems() {
if (!topicList.isEmpty()) {
if (currentItemIndex < topicList.size() - 1) {
currentItemIndex++;
} else {
// Handle when trying to go beyond the last item
Toast.makeText(this, "Already at the last item", Toast.LENGTH_SHORT).show();
return;
}

updateDisplayedItem();
}
}

private void updateDisplayedItem() {
if (currentItemIndex >= 0 && currentItemIndex < topicList.size()) {
chapterListDAdapter.updateItem(topicList.get(currentItemIndex), 0);
}
}

public static class ChapterListDAdapter extends RecyclerView.Adapter<ChapterListDAdapter.viewholder> {

private final Context context;
private final ArrayList<NotesData> list;

public ChapterListDAdapter(Context context, ArrayList<NotesData> list) {
this.context = context;
this.list = list != null ? list : new ArrayList<>();
}

public void updateItem(NotesData newItem, int position) {
list.set(position, newItem);
notifyItemChanged(position);
}

@NonNull
@Override
public viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.cd, parent, false);
return new viewholder(view);
}

@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull viewholder holder, @SuppressLint("RecyclerView") int position) {
NotesData post = list.get(position);
holder.binding.topicName.setText(post.getTopicName());
holder.binding.description.setText(post.getDescription());
Glide.with(context).load(post.getImageurl()).into(holder.binding.titleImage);
}

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

public static class viewholder extends RecyclerView.ViewHolder {
public final CdBinding binding;

public viewholder(@NonNull View itemView) {
super(itemView);
binding = CdBinding.bind(itemView);
}
}
}
}

Comments