Java-Recycleview ViewHolder OnClick也不起作用viewholder.category_name.settext也不起作用

时间:2019-11-08 05:15:57

标签: java android android-recyclerview android-viewholder

我已经尝试了以下代码,但是viewHolder.category_name.setText()无法正常工作,同时viewHolder.setItemclickListener也无法正常工作

类别片段代码

package com.kunal.quiz;


import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.kunal.quiz.Common.Common;
import com.kunal.quiz.Interface.ItemClickListener;
import com.kunal.quiz.ViewHolder.CategoryViewHolder;
import com.shashank.sony.fancytoastlib.FancyToast;
import com.squareup.picasso.Picasso;


/**
 * A simple {@link Fragment} subclass.
 */
public class CategoryFragment extends Fragment {

    View myFragment;
    RecyclerView listCategory;
    RecyclerView.LayoutManager layoutManager;
    FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;

    FirebaseDatabase database;
    DatabaseReference categories;

    public static CategoryFragment newInstance() {
        CategoryFragment categoryFragment = new CategoryFragment();
        return categoryFragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        database = FirebaseDatabase.getInstance();
        categories = database.getReference("Category");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        myFragment = inflater.inflate(R.layout.fragment_category, container, false);
        listCategory = myFragment.findViewById(R.id.list_category);
        listCategory.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(container.getContext());
        listCategory.setLayoutManager(layoutManager);
        loadCategories();
        return myFragment;
    }

    private void loadCategories() {
        adapter = new FirebaseRecyclerAdapter<Category, CategoryViewHolder>(
                Category.class,
                R.layout.category_layout,
                CategoryViewHolder.class,
                categories
        ) {
            @Override
            protected void populateViewHolder(CategoryViewHolder viewHolder,Category model, int position) {
                viewHolder.category_name.setText(model.getName());
                Picasso.with(getActivity())
                        .load(model.getImage())
                        .into(viewHolder.category_image);

                viewHolder.setItemClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
//                        FancyToast.makeText(getActivity(),String.format("%d|%s",adapter.getRef(position).getKey(),model.getName()),FancyToast.LENGTH_LONG,FancyToast.SUCCESS,true).show();
                        Intent startGame = new Intent(getActivity(),Start.class);
                        Common.categoryId = adapter.getRef(position).getKey();
                        startActivity(startGame);
                        FancyToast.makeText(getActivity(),model.getName(),FancyToast.LENGTH_LONG,FancyToast.INFO,true).show();
                    }
                });
            }
        };
        adapter.notifyDataSetChanged();
        listCategory.setAdapter(adapter);
    }

}

这是类别模型代码

package com.kunal.quiz;

public class Category {
    private String Name ;
    private String Image;

    public Category() {
    }

    public Category(String name, String image) {
        Name = name;
        Image = image;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getImage() {
        return Image;
    }

    public void setImage(String image) {
        Image = image;
    }
}

我添加了类别片段的代码,请参见。上载了完整的活动代码,其中提到model.getName()不起作用

1 个答案:

答案 0 :(得分:0)

使用此

public class Category {
      private String Name ; 
      private String Image; 
      public Category() { } 
      public Category(String name, String image) {
             this.Name = name;
             this.Image = image; 
      } 
      public String getName() { return Name; } 
      public void setName(String name) { 
             this.Name = name; 
      } 
      public String getImage() { return Image; 
      }

      public void setImage(String image){
             this.Image = image;
      }
}