-2

hi I'm making a cheats app which have cheat codes for a game (gtav), how can I set a different onclicklistener to each button in recyclerView. For example I have a button in recyclerView called copytoclip board and I want each button in the recyclerView to copy a different text. How can I do that?

recycler_view_list

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">

<TextView
    android:id="@+id/title"
    android:textSize="16dp"
    android:textStyle="bold"
    android:layout_alignParentTop="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/cheat"
    android:layout_below="@id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/description"
    android:layout_below="@+id/cheat"
    android:layout_gravity="bottom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:text="Copy"

    android:id="@+id/cpytocp"
    android:layout_gravity="top|right" />

</LinearLayout>

list

public class Cheats {
    private String title, cheat, description;

    public Cheats(){

    }
    public Cheats( String title, String cheat, String description){
        this.title = title;
        this.cheat = cheat;
        this.description = description;
    }

    public String getTitle(){
        return title;
    }
    public void setTitle(String name){
        this.title = name;
    }
    public String getCheat(){
        return cheat;
    }
    public void setCheat(String cheat){
        this.cheat = cheat;
    }
    public String getDescription(){
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

adapter

`public class CheatsAdapter extends` `RecyclerView.Adapter`<CheatsAdapter.MyViewHolder> `{`

private List<Cheats> cheatList;
public class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView title, cheat, description;

    public MyViewHolder(View view){
        super(view);
        title = (TextView)view.findViewById(R.id.title);
        cheat = (TextView) view.findViewById(R.id.cheat);
        description = (TextView) view.findViewById(R.id.description);
    }
}

public CheatsAdapter(List<Cheats> cheatList){
    this.cheatList = cheatList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cheat_list, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Cheats cheats = cheatList.get(position);
    holder.title.setText(cheats.getTitle());
    holder.cheat.setText(cheats.getCheat());
    holder.description.setText(cheats.getDescription());
}

@Override
public int getItemCount(){
    return cheatList.size();
}
}
himanshu1496
  • 1,921
  • 19
  • 34
Tarek Zoubi
  • 53
  • 1
  • 9

3 Answers3

0
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Cheats cheats = cheatList.get(position);
    holder.title.setText(cheats.getTitle());
    holder.cheat.setText(cheats.getCheat());
    holder.description.setText(cheats.getDescription());

    holder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                // TO DO
                // if this happens to all the buttons..insert here a switch recognizing which button is pressed through getting an info of the button maybe

                }
            });

}
Antonios Tsimourtos
  • 1,676
  • 1
  • 14
  • 37
0

change into your adapter class

  public class CheatsAdapter extends` `RecyclerView.Adapter`<CheatsAdapter.MyViewHolder> `{`

    private List<Cheats> cheatList;
    public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView title, cheat, description;
        public Button btnClick;
        public MyViewHolder(View view){
            super(view);
            btnClick= (Button )view.findViewById(R.id.cpytocp);

            title = (TextView)view.findViewById(R.id.title);
            cheat = (TextView) view.findViewById(R.id.cheat);
            description = (TextView) view.findViewById(R.id.description);
        }
    }

    public CheatsAdapter(List<Cheats> cheatList){
        this.cheatList = cheatList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cheat_list, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Cheats cheats = cheatList.get(position);
btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do that you want to perform on click
            }
        });        holder.title.setText(cheats.getTitle());
        holder.cheat.setText(cheats.getCheat());
        holder.description.setText(cheats.getDescription());
    }

    @Override
    public int getItemCount(){
        return cheatList.size();
    }
    }
Dinesh Saini
  • 335
  • 1
  • 12
0

Add onclick event in the button xml android:onClick="doSomething"

<Button
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:text="Copy"
  android:id="@+id/cpytocp"
  android:layout_gravity="top|right" 
  android:onClick="copyToClipboard" />

Then you need to create the referenced method in your activity / fragment:

public void copyToClipboard(View v) {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
    ClipData clip = ClipData.newPlainText(label, text);
    clipboard.setPrimaryClip(clip);
}

NOTES:

  • If you want to manage various buttons / elements you must add a switch(v.getId()) to know which element was clicked
  • copyToClipboard code is taken as example from here
Graham
  • 7,431
  • 18
  • 59
  • 84
joc
  • 1,336
  • 12
  • 31
  • @TarekZoubi as stated in notes, the code inside listener is just an example, your question is about add a listener, if you get this error, listener is working thus your original problem is solved.... now if you want now to solve problems with clipboard please check answer I linked **or** start a new question and share with me here the link (don't modify question with new issues, [*Don't be a chameleon, don't be a vandal*](http://meta.stackoverflow.com/questions/332820/what-to-do-when-someone-answers-dont-be-a-chameleon-dont-be-a-vandal) – joc Aug 26 '16 at 08:50
  • @TarekZoubi from answer I linked *make sure you have imported `android.content.ClipboardManager` and **NOT** `android.text.ClipboardManager`. Latter is deprecated.* – joc Aug 26 '16 at 08:53