Как передать ссылку на String [] htmlFile в методе / объекте адаптера RecyclerView во фрагменте - PullRequest
0 голосов
/ 18 июня 2020

введите здесь описание изображения Я пытаюсь показать Html файл, используя RecyclerView внутри фрагмента, но когда я передаю ссылку String [] htmlFile методу recyclerViewAdapter во фрагменте, ссылка не проходит ... Я стараюсь изо всех сил, но терплю неудачу ... Помогите мне ... Будет вам очень полезно ... Вот мой код RecyclerViewAdapter

package UserInterface.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.example.knowledgebite.R;

import java.util.ArrayList;
import java.util.Collection;


public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> implements Filterable {

    Context context;        //variable of context to get the context of the adapter class
    ArrayList<TopicsName> dataList;     //make an ArrayList variable called dataList and pass the TopicsName Class
    ArrayList<TopicsName> dataListAll;  //make another ArrayList variable called dataList and pass the TopicsName Class
    RecyclerViewAdapterEvents recyclerViewAdapterEvents; // make an object of RecyclerViewAdapterEvents
    String[] htmlFile;

    //constructor to get the data which shows in recyclerView
    public RecyclerViewAdapter(RecyclerViewAdapterEvents recyclerViewAdapterEvents, ArrayList<TopicsName> dataList, , String[] htmlFile,Context context) {
        this.recyclerViewAdapterEvents = recyclerViewAdapterEvents;
        this.dataList = dataList;
        this.context = context;
        this.htmlFile = htmlFile;
        dataListAll = new ArrayList<>(dataList);
    }

    /* this function create the views which we want and store all the views in the view holder
    it also store references so that we don't have to call findViewByID again and again */
    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(context).inflate(R.layout.list_item_layout, parent, false);
        RecyclerViewHolder viewHolder = new RecyclerViewHolder(view, recyclerViewAdapterEvents);
        return viewHolder;
    }

    /*this function binds the data..
    dataBinding allows you to bind UI components in your layouts to data sources in your app
    using a declarative format rather than programmatically..
    Here we also set the data of view..*/
    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {

        String htmlFiles = htmlFile[position];
        holder.textView.setText(dataList.get(position).getName());

    }

    //this function shows that what is the itemCount of the data u want to show in the recyclerView
    @Override
    public int getItemCount() {
        return dataList.size();
    }

    //this function gets the search filter
    @Override
    public Filter getFilter() {
        return filter;
    }

    private Filter filter = new Filter() {
        @Override
        //run on a background thread
        protected FilterResults performFiltering(CharSequence constraint) {
            ArrayList<TopicsName> filteredList = new ArrayList<>();
            if (constraint == null || constraint.length() == 0) {
                filteredList.addAll(dataListAll);
            } else {
                for (TopicsName data : dataListAll) {
                    if (data.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                        filteredList.add(data);
                    }

                }
            }

            FilterResults results = new FilterResults();
            results.values = filteredList;

            return results;
        }

        //run on a ui thread
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            dataList.clear();
            dataList.addAll((Collection<? extends TopicsName>) results.values);
            notifyDataSetChanged();

        }
    };

    //this is the nested that inherit ViewHolder class that retains all the views so that we can recycle these views
    public class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        //here are the references that use by onCreateViewHolder function
        private TextView textView;
        private ImageView imageView;
        RecyclerViewAdapterEvents recyclerViewAdapterEvents; // make an object of RecyclerViewAdapterEvents

        public RecyclerViewHolder(@NonNull View itemView, RecyclerViewAdapterEvents recyclerViewAdapterEvents) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.list_tex_title);
            imageView = (ImageView) itemView.findViewById(R.id.ivArrow);

            this.recyclerViewAdapterEvents = recyclerViewAdapterEvents;

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            //int position = getAdapterPosition();
            //recyclerViewAdapterEvents.onSubjectClick(dataList.get(position));
            recyclerViewAdapterEvents.onSubjectClick(getAdapterPosition());

        }
    }

    //interface for click handling on each item of recyclerView
    public interface RecyclerViewAdapterEvents {
        void onSubjectClick(int position);
    }

}

А это мой код фрагмента

package UserInterface.Fragments;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;

import com.example.knowledgebite.R;

import UserInterface.Adapters.RecyclerViewAdapter;
import UserInterface.Adapters.TopicsName;

import java.util.ArrayList;

import UserInterface.Activities.LearnCAMcqActivity;


/**
 * A simple {@link Fragment} subclass.
 */
public class LearnDetailComputerArchitectureFragment extends Fragment implements RecyclerViewAdapter.RecyclerViewAdapterEvents {

    View view;  //variable of View type
    ArrayList<TopicsName> dataList;    //make an ArrayList variable called topic and pass the TopicsName Class
    RecyclerView recyclerView;     //variable of recyclerView
    RecyclerViewAdapter recyclerViewAdapter; //make an object of RecyclerViewAdapter


    public LearnDetailComputerArchitectureFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        dataList = new ArrayList<>();
        dataList.add(new TopicsName("Functional Units of a Computer"));
        dataList.add(new TopicsName("Basic Operational Concept"));
        dataList.add(new TopicsName("BUS Structure"));
        dataList.add(new TopicsName("Performance of a System"));
        dataList.add(new TopicsName("Addressing Modes"));
        dataList.add(new TopicsName("Numbers and Arithmetic Operations"));
        dataList.add(new TopicsName("Memory Locations and Addresses"));
        dataList.add(new TopicsName("Memory Operations and Management"));
        dataList.add(new TopicsName("Instructions & Instruction Sequencing"));
        dataList.add(new TopicsName("Assembly Language"));
        dataList.add(new TopicsName("Subroutines and Nesting"));
        dataList.add(new TopicsName("Parameter Passing and Stack Frame"));
        dataList.add(new TopicsName("Accessing I/O Devices"));
        dataList.add(new TopicsName("Interrupts – 1"));
        dataList.add(new TopicsName("Interrupts – 2"));
        dataList.add(new TopicsName("Exceptions"));
        dataList.add(new TopicsName("Direct Memory Access"));
        dataList.add(new TopicsName("Bus Arbitration"));
        dataList.add(new TopicsName("Synchronous BUS"));
        dataList.add(new TopicsName("Asynchronous BUS"));
        dataList.add(new TopicsName("Interface Circuits"));
        dataList.add(new TopicsName("Standard I/O Interfaces"));
        dataList.add(new TopicsName("Parallel Port"));
        dataList.add(new TopicsName("Serial Port"));
        dataList.add(new TopicsName("PCI BUS"));
        dataList.add(new TopicsName("SCSI BUS"));
        dataList.add(new TopicsName("USB"));
        dataList.add(new TopicsName("Static Memories"));
        dataList.add(new TopicsName("Asynchronous DRAM"));
        dataList.add(new TopicsName("Synchronous DRAM"));
        dataList.add(new TopicsName("Large Memories"));
        dataList.add(new TopicsName("RamBus Memory"));
        dataList.add(new TopicsName("Read-Only Memory"));
        dataList.add(new TopicsName("Hierarchy of Memory"));
        dataList.add(new TopicsName("Caches"));
        dataList.add(new TopicsName("Mapping Functions"));
        dataList.add(new TopicsName("Cache Miss and Hit"));
        dataList.add(new TopicsName("Single BUS Organisation"));
        dataList.add(new TopicsName("Multiple BUS Organisation"));
        dataList.add(new TopicsName("Hardwired Control"));
        dataList.add(new TopicsName("Micro-programmed Control"));
        dataList.add(new TopicsName("Replacement Algorithms"));
        dataList.add(new TopicsName("Performance of Caches"));
        dataList.add(new TopicsName("Virtual Memory"));
        dataList.add(new TopicsName("Secondary Storage"));
        dataList.add(new TopicsName("Fast Adders"));
        dataList.add(new TopicsName("Multiplication"));
        dataList.add(new TopicsName("Representation of Floating Number"));
        dataList.add(new TopicsName("Pipelining"));
        dataList.add(new TopicsName("Superscalar Processors"));
        dataList.add(new TopicsName("CISC and RISC Processors"));
        dataList.add(new TopicsName("Hazards of Processor Architecture"));
        dataList.add(new TopicsName("Clusters"));
        dataList.add(new TopicsName("VLIW Architecture (I-64)"));
        dataList.add(new TopicsName("Address Translation"));
        dataList.add(new TopicsName("Motorola 680X0 Processor architecture"));
        dataList.add(new TopicsName("ARM architecture"));
        dataList.add(new TopicsName("Intel IA-32 Pentium Architecture"));

        setHasOptionsMenu(true);        //used to get optionMenu in a fragment

        super.onCreate(savedInstanceState);



    }

    //function for searchView in a recycler View
    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {

        inflater.inflate(R.menu.main_menu, menu);

        MenuItem item = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) item.getActionView();

        //to change the keyboard option
        searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);

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

            @Override
            public boolean onQueryTextChange(String newText) {
                recyclerViewAdapter.getFilter().filter(newText);
                return false;
            }
        });
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_learn_detail_computer_architecture, container, false);
        //cast the recyclerView
        recyclerView = (RecyclerView) view.findViewById(R.id.CA_Detail_RecyclerView);
        //cast object of recyclerView
        recyclerViewAdapter = new RecyclerViewAdapter(this, dataList,htmlFile, getContext());
        //set the layout of recyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //to divide a list in a recyclerView
        RecyclerView.ItemDecoration divider = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(divider);
        //set the adapter of recyclerView
        recyclerView.setAdapter(recyclerViewAdapter);
        return view;

    }

    @Override
    public void onSubjectClick(int position) {
        //dataList.get(position);       this is the stament to get the adapter position
        Intent intent = new Intent(getActivity(), LearnCAMcqActivity.class);
        startActivity(intent);

        //this is the code if we want to move different activity from each of the item of recyclerView
     /*   final Intent intent;
        if (position == 0){
            intent =  new Intent(getActivity(), LearnCAMcqActivity.class);
        } else if (position == 1){
            intent =  new Intent(getActivity(), LearnCAMcq2Activity.class);
        } else {
            intent =  new Intent(getActivity(), LearnCAMcq3Activity.class);
        }
        startActivity(intent);
    */
    }
}

1 Ответ

0 голосов
/ 18 июня 2020

Здесь вы передаете свой htmlFile в RecyclerView

recyclerViewAdapter = new RecyclerViewAdapter(this, dataList,htmlFile, getContext());

Где инициализируется htmlFile ??

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...