java – First Time show fetch data on recyclerview but the second time open recycleview not show data-ThrowExceptions

Exception or error:

hi i am developing app to face some problems my problem is if first time open recycleview to fetch data show full correct means first time not problem correct show data but the app is close again reopen recyclerview to not show data. i am using mysql database on working retrofit library
thanx advance.

    RecyclerView recyclerView;
    EditText searchcard;
    Superdent_Invi_fetct.Adapter adapter;
    FloatingActionButton rotationbtn;
    List<Supert_Invigilator_Model> data;
    String supert_id, hall_id;
    LinearLayoutManager layoutManager;

    SharedPreferences sharedPreferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_superdent__invi_fetct);


        supert_id = getIntent().getStringExtra("id");
        hall_id = getIntent().getStringExtra("hall_id");

        rotationbtn = findViewById(R.id.rotat_float_btn);
        searchcard = (EditText) findViewById(R.id.searchcard);
        recyclerView = (RecyclerView) findViewById(R.id.std_report_list);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        searchcard.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }


            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.e("onTextChanged: ", s.toString());

                if (adapter != null) {
                    if (!s.toString().isEmpty()) {
                        Filter filter = adapter.getFilter();
                        filter.filter(s.toString());
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    searchcard.setError("List is Empty");
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                Filter filter = adapter.getFilter();
                filter.filter(s.toString());
            }
        });


        rotationbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                screenscape();
            }
        });
        ShowReport();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 555:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
//                    createShopingPdf();

                } else {
                    // Permission Denied
                    Toast.makeText(this, "WRITE_EXTERNAL Permission Denied", Toast.LENGTH_SHORT)
                            .show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    public void ShowReport() {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", supert_id);
        map.put("hall_id", hall_id);
        Invi_std_Api apiInterface = ApiClient.getClient().create(Invi_std_Api.class);
        Call<List<Supert_Invigilator_Model>> call = apiInterface.getsuperdentinvigilator(map);
        call.enqueue(new Callback<List<Supert_Invigilator_Model>>() {
            @Override
            public void onResponse(Call<List<Supert_Invigilator_Model>> call, Response<List<Supert_Invigilator_Model>> response) {
                data = response.body();
                Log.e("size", "onResponse: " + data.size());


                adapter = new Superdent_Invi_fetct.Adapter(Superdent_Invi_fetct.this, data);
                recyclerView.setAdapter(adapter);

            }

            @Override
            public void onFailure(Call<List<Supert_Invigilator_Model>> call, Throwable t) {
                Log.e("Exception", "onFailure: " + t.getMessage());

            }
        });
    }

    public void screenscape() {


        int orientation = getResources().getConfiguration().orientation;
        switch (orientation) {
            case Configuration.ORIENTATION_LANDSCAPE:
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    }
                });


                break;

            case Configuration.ORIENTATION_PORTRAIT:
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    }
                });

                break;
        }
    }


    public class Adapter extends RecyclerView.Adapter<Superdent_Invi_fetct.Adapter.MyViewHolder> implements Filterable {
        Context context;
        List<Supert_Invigilator_Model> data;
        List<Supert_Invigilator_Model> copyList;


        public Adapter(Context context, List<Supert_Invigilator_Model> data) {
            this.context = context;
            this.data = data;
            this.copyList = data;
        }

        @NonNull
        @Override
        public Superdent_Invi_fetct.Adapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.invigilator_record_spt, viewGroup, false);

            return new Superdent_Invi_fetct.Adapter.MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull Superdent_Invi_fetct.Adapter.MyViewHolder holder, int i) {

            final Supert_Invigilator_Model model = data.get(i);
            //  holder.tvid.setText(copyList.get(i).getInvigId());
            holder.tvinvname.setText(copyList.get(i).getName());
            holder.tvhall.setText(copyList.get(i).getTitle());
            holder.tvroleid.setText(copyList.get(i).getRoleId());
            holder.tvhallid.setText(copyList.get(i).getHallId());


        }

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

        @Override
        public Filter getFilter() {

            Filter filter = new Filter() {

                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {

                    copyList = (List<Supert_Invigilator_Model>) results.values;
                    notifyDataSetChanged();
                }

                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults results = new FilterResults();
                    List<Supert_Invigilator_Model> FilteredArrList = new ArrayList<>();

                    if (data == null) {
                        data = new ArrayList<>();
                    }

                    if (constraint == null || constraint.length() == 0) {
                        results.count = data.size();
                        results.values = data;
                    } else {
                        constraint = constraint.toString().toLowerCase();
                        for (int i = 0; i < data.size(); i++) {
                            String item = data.get(i).getName();

                            if (item.toLowerCase().contains(constraint.toString())) {
                                FilteredArrList.add(new Supert_Invigilator_Model(
                                        data.get(i).getCnic(),
                                        data.get(i).getDeptId(),
                                        data.get(i).getFName(),
                                        data.get(i).getHallId(),
                                        data.get(i).getId(),
                                        data.get(i).getInvigId(),
                                        data.get(i).getName(),
                                        data.get(i).getPassword(),
                                        data.get(i).getRoleId(),
                                        data.get(i).getRoll(),
                                        data.get(i).getSupertId(),
                                        data.get(i).getTitle(),
                                        data.get(i).getUserId()));
                            }
                        }
                        results.count = FilteredArrList.size();
                        results.values = FilteredArrList;
                    }
                    return results;
                }
            };
            return filter;
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tvid, tvinvname, tvhall, tvroleid, tvhallid;


            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                //    tvid = itemView.findViewById(R.id.tID);
                tvinvname = itemView.findViewById(R.id.tIviglatorName);
                tvhall = itemView.findViewById(R.id.tHallName);
                tvroleid = itemView.findViewById(R.id.troleid);
                tvhallid = itemView.findViewById(R.id.thallid);


            }

            public List<Supert_Invigilator_Model> getList() {
                Log.e("getList: ", "" + copyList);
                return copyList;

            }
        }
    }
}
How to solve:

Try calling adapter.notifyDataSetChanged(); in your ShowReport method after setting the adapter.

Also, don’t try to set your adapter in your ShowReport method. Try to set it in onCreate using another list and just add the response in that list in your ShowReport method.

And also, please try to debug the code so that you can know why it’s not showing the data in the second run.

Leave a Reply

Your email address will not be published. Required fields are marked *