转接呼叫无效

时间:2019-07-22 22:04:35

标签: android recycler-adapter

我是Android的初学者,所以对不起我的noobie问题。我的应用程序中有RecyclerView个列表。我为此创建了DataAdapter和Activity。我还创建了两个布局: favorite_items (分配给数据适配器)和 favorite_activity (分配给Activity)。在favourite_items布局(数据适配器)中,我有“共享”按钮,在“活动”中,我有功能(ButtonLogic)。我想从活动中调用函数,但我不知道如何。我必须补充一点,RecyclerView可以正常工作。

活动

public void ButtonsLogic(){

   mShare.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          String s = Textview.getText().toString();
          Intent sharingIntent = new Intent(Intent.ACTION_SEND);
          sharingIntent.setType("text/plain");
          sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
          sharingIntent.putExtra(Intent.EXTRA_TEXT, s);
          startActivity(Intent.createChooser(sharingIntent, "Share data:"));

      }
});

适配器

public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.FavoriteViewHolder> {


    //this context we will use to inflate the layout
    private Context mCtx;

    //we are storing all the products in a list
    private List<Favorite> favoriteList;

    //getting the context and product list with constructor
    public FavoriteAdapter(Context mCtx, List<Favorite> favoriteList {
        this.mCtx = mCtx;
        this.favoriteList= favoriteList;
    }

    @Override
    public FavoriteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflating and returning our view holder
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.favorite_items, null);
        return new FavoriteViewHolder(view);
    }

    @Override
    public void onBindViewHolder(FavoriteViewHolder holder, int position) {
        //getting the product of the specified position
        Favorite favorite= quoteModelList.get(position);

        //binding the data with the viewholder views
        holder.Favoritetext.setText(favorite.getFavoriteText());


    }


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


    class FavoriteViewHolder extends RecyclerView.ViewHolder {

        TextView Favoritetext;


        public FavoriteViewHolder(View itemView) {
            super(itemView);
            Favoritetext = itemView.findViewById(R.id.favoritetextview);


        }
    }
}

1 个答案:

答案 0 :(得分:1)

创建这样的界面

interface Listener {
    private void onItemClicked(String text);
}

并在您的Activity中实施。构建适配器时,请通过此接口

public FavoriteAdapter(Context mCtx, List<Favorite> favoriteList, Listener listener) {
    this.mCtx = mCtx;
    this.favoriteList= favoriteList;
    this.listener = listener

}

在绑定方法中设置侦听器

@Override
public void onBindViewHolder(FavoriteViewHolder holder, int position) {
    //getting the product of the specified position
    Favorite favorite= quoteModelList.get(position);

    //binding the data with the viewholder views


    holder.Favoritetext.setText(favorite.getFavoriteText());
    holder.button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            listener.onIemClicked(/* get text to pass to activity */);
        }
    };
}
相关问题