Как исправить несовместимые типы: фрагмент невозможно преобразовать в контекст - PullRequest
0 голосов
/ 02 августа 2020

Я пытаюсь просмотреть элементы из своей базы данных, и я написал все, чтобы получить их и отобразить на странице действий. но эта одна строка кода дает ошибку, которая не позволяет запускать приложение:

вот мой подробный код:

public class HomeFragment extends Fragment {

    private String name;
    private String location;
    private String country;
    private String address;

    public HomeFragment() {

    }

    public HomeFragment(String name, String location, String country, String address) {
        this.name = name;
        this.location = location;
        this.country = country;
        this.address = address;


    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


    private FirebaseFirestore firebaseFirestore;
    private RecyclerView FirestoreList;

    private FirestoreRecyclerAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        firebaseFirestore = FirebaseFirestore.getInstance();
       FirestoreList = FirestoreList.findViewById(R.id.firestore_list);

        //query
        Query q = firebaseFirestore.collection("Shops");

        //recycle options
        FirestoreRecyclerOptions<ShopModel> options = new FirestoreRecyclerOptions.Builder<ShopModel>().setQuery(q, ShopModel.class).build();

         adapter = new FirestoreRecyclerAdapter<ShopModel, ShopViewHolder>(options) {
            @NonNull
            @Override
            public ShopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_home,parent,false);
                return new ShopViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(@NonNull ShopViewHolder holder, int position, @NonNull ShopModel model) {
                holder.list_name.setText(model.getName());
                holder.list_country.setText(model.getName());
                holder.list_location.setText(model.getName());
                holder.list_address.setText(model.getName());
            }
        };
        FirestoreList.setHasFixedSize(true);

        FirestoreList.setLayoutManager(new LinearLayoutManager(this));// this is the line that is giving me the error

        FirestoreList.setAdapter(adapter);



        return inflater.inflate(R.layout.fragment_home, container, false);

    }


    private class ShopViewHolder extends RecyclerView.ViewHolder {
        private TextView list_name;
        private TextView list_country;
        private TextView list_location;
        private TextView list_address;
        public ShopViewHolder(@NonNull View itemView) {
            super(itemView);

            list_name = itemView.findViewById(R.id.list_name);
            list_country = itemView.findViewById(R.id.list_country);
            list_location = itemView.findViewById(R.id.list_location);
            list_address = itemView.findViewById(R.id.list_address);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

это строка, где есть проблема в

FirestoreList.setLayoutManager(new LinearLayoutManager(this)); // это строка, которая выдает ошибку

что я могу сделать, чтобы решить эту проблему?

Ответы [ 2 ]

1 голос
/ 02 августа 2020

Вы передаете свой фрагмент LinearLayoutManager, а не Context

Итак, вам нужно просто заменить эту строку

FirestoreList.setLayoutManager(new LinearLayoutManager(this));

на

FirestoreList.setLayoutManager(new LinearLayoutManager(requireContext()));
0 голосов
/ 02 августа 2020

Не могли бы вы создать объект для FirestoreList и попытаться установить LayoutManager для этого объекта?

FirestoreList fsList = FirestoreList.findViewById(R.id.firestore_list);
...
fsList.setLayoutManager(new LinearLayoutManager(this));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...