如何修复不兼容的类型:无法将片段转换为上下文。

huangapple go评论80阅读模式
英文:

How to fix incompatible types: fragment cannot be converted to Context

问题

这是出现问题的那一行代码:

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

要解决这个问题,你可以尝试将 this 更改为 requireContext(),如下所示:

FirestoreList.setLayoutManager(new LinearLayoutManager(requireContext()));

这样应该可以解决你的错误。

英文:

i am trying to view items from my database and i have written everything to retrieve that and display it in an activity page. but this one line of code gives an error that doesn't allow the app to run:

here is my detailed code:

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();
    }
}

this is the line where there is a problem at

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

what can i do to resolve this?

答案1

得分: 2

你正在将你的片段传递给 LinearLayoutManager,而不是 Context

所以你需要做的就是简单地替换这行代码

FirestoreList.setLayoutManager(new LinearLayoutManager(this));

FirestoreList.setLayoutManager(new LinearLayoutManager(requireContext()));
英文:

You are passing your fragment to LinearLayoutManager, not the Context

So what you need to do is simply replace this line

FirestoreList.setLayoutManager(new LinearLayoutManager(this));

To

FirestoreList.setLayoutManager(new LinearLayoutManager(requireContext()));

答案2

得分: 0

请创建一个名为FirestoreList的对象,并尝试将布局管理器设置为该对象:

FirestoreList fsList = FirestoreList.findViewById(R.id.firestore_list);
...
fsList.setLayoutManager(new LinearLayoutManager(this));
英文:

Can you please make an object for FirestoreList and try to set the LayoutManager to the object ?

FirestoreList fsList = FirestoreList.findViewById(R.id.firestore_list);
...
fsList.setLayoutManager(new LinearLayoutManager(this));

答案3

得分: 0

将这个语句更改为:

View view = inflater.inflate(R.layout.fragment_home, container, false);
英文:

Change this statement :
return inflater.inflate(R.layout.fragment_home, container, false);

to:
View view = inflater.inflate(R.layout.fragment_home, container, false);

huangapple
  • 本文由 发表于 2020年8月2日 20:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63215770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定