Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / views / MyAdapter.java
//package com.example.citrusclient.views;
//
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.TextView;
//
//import androidx.recyclerview.widget.RecyclerView;
//
//import com.example.citrusclient.R;
//import com.example.citrusclient.models.Book;
//
//import java.util.ArrayList;
//import java.util.List;
//
//public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
//    private List<Book> originalList; // 元のデータリスト
//    private List<Book> filteredList;
//
//    public MyAdapter(List<Book> originalList) {
//        this.originalList = originalList;
//        this.filteredList = new ArrayList<>(originalList);
//    }
//
//    @Override
//    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_public_book, parent, false);
//        return new ViewHolder(view);
//    }
//
//    @Override
//    public void onBindViewHolder(ViewHolder holder, int position) {
//        Book currentBook = filteredList.get(position);
//        holder.bind(currentBook);
//    }
//
//    @Override
//    public int getItemCount() {
//        return filteredList.size();
//    }
//
//    public void filter(String query) {
//        filteredList.clear();
//        if (query.isEmpty()) {
//            filteredList.addAll(originalList);
//        } else {
//            String[] queryWords = query.toLowerCase().trim().split("\\s+");
//            for (Book item : originalList) {
//                boolean matchesAll = true;
//                for (String word : queryWords) {
//                    if (!item.getAccountId().toLowerCase().contains(word)) {
//                        matchesAll = false;
//                        break;
//                    }
//                }
//                if (matchesAll) {
//                    filteredList.add(item);
//                }
//            }
//        }
//        notifyDataSetChanged();
//    }
//
//    public void setBooks(ArrayList publicList) {
//    }
//
//    static class ViewHolder extends RecyclerView.ViewHolder {
//        TextView titleTextView;
//        TextView authorTextView;
//
//        ViewHolder(View itemView) {
//            super(itemView);
//            titleTextView = itemView.findViewById(R.id.public_button);
//            authorTextView = itemView.findViewById(R.id.public_id);
//        }
//
//        void bind(Book book) {
//            titleTextView.setText(book.getTitle());
//            authorTextView.setText(book.getAccountId());
//        }
//    }
//}

package com.example.citrusclient.views;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.citrusclient.R;
import com.example.citrusclient.models.Book;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<Book> originalList; // 元のデータリスト
    private List<Book> filteredList; // フィルタリングされたリスト

    public MyAdapter(List<Book> originalList) {
        this.originalList = originalList;
        this.filteredList = new ArrayList<>(originalList);
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Book currentBook = filteredList.get(position);
        holder.bind(currentBook);
    }

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

    // 新しいデータを設定するメソッド
    public void setBooks(List<Book> books) {
        originalList.clear();
        originalList.addAll(books);
        filter(""); // 空のクエリで全てのアイテムを表示
    }

    // フィルタリングメソッド
    public void filter(String query) {
        filteredList.clear();
        if (query.isEmpty()) {
            filteredList.addAll(originalList);
        } else {
            String lowerCaseQuery = query.toLowerCase().trim();
            for (Book item : originalList) {
                if (item.getAccountId().toLowerCase().contains(lowerCaseQuery) ||
                        item.getTitle().toLowerCase().contains(lowerCaseQuery)) {
                    filteredList.add(item);
                }
            }
        }
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView titleTextView;
        TextView authorTextView;

        ViewHolder(View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.public_button);
            authorTextView = itemView.findViewById(R.id.public_id);
        }

        void bind(Book book) {
            titleTextView.setText(book.getTitle());
            authorTextView.setText(book.getAccountId());
        }
    }
}