Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

android - 未连接适配器; 在Fragment,RecyclerView中跳过布局(No adapter attached; skipping layout in Fragment, RecyclerView)

Recyclerview in my app is not working.

(我的应用程序中的Recyclerview无法正常工作。)

Fragment is giving me this error but I couldn't figure it out.

(片段给了我这个错误,但我无法弄清楚。)

I know that there are lots of questions about this issue but none of them solved my problem so far.

(我知道关于这个问题有很多问题,但是到目前为止,没有一个问题解决了我的问题。)

Fragment Code:

(片段代码:)

public class PatientsFragment extends Fragment {

    private FragmentActivity myContext;
    ClinicViewModel clinicViewModel;

    @Override
    public void onAttach(Activity activity) {
        myContext = (FragmentActivity) activity;
        super.onAttach(activity);
    }


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_patients, container, false);

        FloatingActionButton fab = root.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // -> Dodawanie nowego pacjenta
                FrameLayout host = getView().findViewById(R.id.nav_host_fragment);
                //host.removeAllViews();
                AddPatientFragment addPatientFragment = new AddPatientFragment();
                FragmentManager fm = myContext.getSupportFragmentManager();
                fm.beginTransaction()
                        .replace(R.id.nav_host_fragment, addPatientFragment)
                        .addToBackStack(null)
                        .commit();
            }
        });

        View rootView = inflater.inflate(R.layout.fragment_patients, container, false);

        RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerViewPatients);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);

        final PatientsAdapter adapter = new PatientsAdapter();
        recyclerView.setAdapter(adapter);

        clinicViewModel =   ViewModelProviders.of(this).get(ClinicViewModel.class);
        clinicViewModel.getAllPatients().observe(this, new Observer<List<Patient>>() {
            @Override
            public void onChanged(List<Patient> patients) {
                //update Recycler todo
                Toast.makeText(getContext(), "onChanged", Toast.LENGTH_SHORT).show();
                adapter.setPatients(patients);
            }
        });



        return root;
    } 

and an Adapter

(和一个适配器)

    private List<Patient> patients = new ArrayList<>();

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

    @Override
    public void onBindViewHolder(@NonNull PatientHolder holder, int position) {
        Patient currentPatient = patients.get(position);
        holder.textViewPersonName.setText(currentPatient.getFirstName() + " " + currentPatient.getLastName());
    }

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

    public void setPatients(List<Patient> patients){
        this.patients = patients;
        notifyDataSetChanged();
    }

    class PatientHolder extends RecyclerView.ViewHolder {
        private TextView textViewPersonName;

        public PatientHolder(@NonNull View itemView) {
            super(itemView);
            textViewPersonName = itemView.findViewById(R.id.textViewPersonName);
        }
    }

}

Like i said before, I can't see anything wrong with this code :( Thanks in advance!

(就像我之前说过的,我看不到这段代码有什么问题:(预先感谢!)

  ask by domvnski translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try using root instead of rootView like below:

(尝试使用root代替rootView如下所示:)

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_patients, container, false);

    ....

    //View rootView = inflater.inflate(R.layout.fragment_patients, container, false);

    RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recyclerViewPatients);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setHasFixedSize(true);

    final PatientsAdapter adapter = new PatientsAdapter();
    recyclerView.setAdapter(adapter);

    ....

    return root;
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...