第一次在 recyclerview 上显示获取数据,但第二次打开 recyclerview 不显示数据

Posted

技术标签:

【中文标题】第一次在 recyclerview 上显示获取数据,但第二次打开 recyclerview 不显示数据【英文标题】:First Time show fetch data on recyclerview but the second time open recycleview not show data 【发布时间】:2020-06-02 10:19:12 【问题描述】:

我正在开发应用程序以面对一些问题我的问题是如果第一次打开 recycleview 以获取数据显示完全正确意味着第一次没有问题正确显示数据但应用程序再次关闭重新打开 recyclerview 以不显示数据。我在工作改造库上使用 mysql 数据库。

    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;

            
        
    

【问题讨论】:

【参考方案1】:

设置适配器后尝试在ShowReport 方法中调用adapter.notifyDataSetChanged();

另外,不要尝试在 ShowReport 方法中设置适配器。尝试使用另一个列表将其设置在 onCreate 中,然后在您的 ShowReport 方法中添加该列表中的响应。

另外,请尝试调试代码,以便您知道为什么它在第二次运行时没有显示数据。

【讨论】:

以上是关于第一次在 recyclerview 上显示获取数据,但第二次打开 recyclerview 不显示数据的主要内容,如果未能解决你的问题,请参考以下文章

SQLite 的 Recyclerview 第一次显示空白页面

kotlin 在片段中的 RecyclerView 中显示来自 Firebase 数据库的数据

recyclerview 上没有显示任何项目

从Firebase实时数据库读取Android数据并在RecyclerView上显示

cursor 第一次为空,recyclerView 不显示游标数据

尝试获取 Firebase 云数据库数据并将其显示在 RecyclerView 中的片段中