如何从RecyclerView Adapter中的Activity访问变量和方法

时间:2017-09-07 13:10:40

标签: android android-recyclerview adapter

我可以在RecylerView Adapter中访问变量和方法吗? 我需要从我将用于OnbindViewHolder的活动中访问变量,我还需要访问另一个活动中的方法。所以任何人都可以告诉我如何做到正确,因为我使用的代码不起作用。

这是RecyclerView适配器上的代码

  public class db_adapter extends RecyclerView.Adapter<db_adapter.pet_viewholder> implements View.OnLongClickListener, View.OnClickListener
{

    ArrayList<db_getItem> items;
    Context ctx;
    HomeActivity homeActivity;
    public db_adapter(Context ctx,ArrayList<db_getItem> items) {
            this.items=items;
            this.ctx=ctx;
            homeActivity=(HomeActivity)ctx;
    }

    @Override
    public pet_viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.reg_view_pet, parent, false);
        return new pet_viewholder(view,homeActivity);

    }
    public void add(db_getItem pet) {
        items.add(0, pet);
        notifyItemInserted(0);

    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(final pet_viewholder holder, final int position) {
        holder.pet_name.setText(items.get(position).getPet_name());
        holder.pet_breed.setText(items.get(position).getPet_breed());
        holder.pet_age.setText(items.get(position).getPet_age());
        holder.pet_bday.setText(items.get(position).getPet_bday());
                String beagle="Beagle";
                String bulldog="Bull Dog";
                String chow="Chow chow";
                String chihua="Chihuahua";
                String german="German Shepherd";
                String golden="Golden Retriever";
                String havanese="Havanese";
                String labrador="Labrador Retriever";
                String shitzu="Shitzu";
                String pug="Pug";

                final String breed=items.get(position).getPet_breed();
                if(beagle.equals(breed)){
                    holder.pet_image.setImageResource(R.drawable.beagle);
                }else if(bulldog.equals(breed)){
                    holder.pet_image.setImageResource(R.drawable.bulldog);
                }else if(chow.equals(breed)){
                    holder.pet_image.setImageResource(R.drawable.chow_chow);
                }else if(chihua.equals(breed)){
                    holder.pet_image.setImageResource(R.drawable.chichua);
                }else if(german.equals(breed)){
                    holder.pet_image.setImageResource(R.drawable.german_shepherd);
                }else if(golden.equals(breed)){
                    holder.pet_image.setImageResource(R.drawable.golden_retriever);
                }else if(havanese.equals(breed)){
                    holder.pet_image.setImageResource(R.drawable.havanese);
                }else if(labrador.equals(breed)){
                    holder.pet_image.setImageResource(R.drawable.labrador);
                }else if(pug.equals(breed)){
                    holder.pet_image.setImageResource(R.drawable.pug);
                }else if(shitzu.equals(breed)) {
                    holder.pet_image.setImageResource(R.drawable.shitzu);
                }
    }

    @Override
    public boolean onLongClick(View v) {
        return false;
    }

    @Override
    public void onClick(View v) {
    }
    @Override
    public int getItemCount() {
        return items.size();
    }

    public static class pet_viewholder extends RecyclerView.ViewHolder {
        CheckBox chregis_pet;
        TextView pet_name;
        TextView pet_breed;
        TextView pet_age;
        TextView pet_bday;
        ImageView pet_image;
        RecyclerView recyclerView;
        View view;
        CardView cardView;
        HomeActivity homeActivity;
        Context data_context;

        public pet_viewholder(View itemView,Context data_context) {
            super(itemView);
            this.data_context=data_context;
            this.homeActivity=(HomeActivity)data_context;
            this.view=itemView.findViewById(R.id.view);
            pet_name=(TextView)itemView.findViewById(R.id.name_pet);
            pet_breed=(TextView)itemView.findViewById(R.id.breed_pet);
            pet_age=(TextView)itemView.findViewById(R.id.age_pet);
            pet_bday=(TextView)itemView.findViewById(R.id.bday_pet);
            pet_image=(ImageView) itemView.findViewById(R.id.pic_breed);
            recyclerView=(RecyclerView)itemView.findViewById(R.id.reg_pet);
            cardView=(CardView)itemView.findViewById(R.id.pet_card);
            chregis_pet=(CheckBox)itemView.findViewById(R.id.chkregis_pet);

        }

    }
}

1 个答案:

答案 0 :(得分:0)

创建一个这样的接口类,

public interface SomeInterface {
void someMethodFromActivity();
}

然后让您的活动实现此界面,如

public class SomeActivity implements SomeInterface {
your code here
}

在此之后,从活动调用适配器的地方,以及传递的上下文和arraylist,传递您的回调方法。像这样的东西

yourAdapterConstructor(context,arraylist,this)

请注意,您已使用此方法传递了context和arrarylist,您还需要添加此回调。在你的适配器类中改变你的构造函数,

private SomeInterface listener;
public db_adapter(Context ctx,ArrayList<db_getItem> items,SomeInterface listener) {
        this.listener=listener;
        this.items=items;
        this.ctx=ctx;
        homeActivity=(HomeActivity)ctx;
}

现在,您可以使用此侦听器方法调用活动中的任何方法,例如

listener.someMethodFromActivity();

请记住在活动类

中定义someMethodFromActivity()