Reputation: 315
I am creating a GridLayout that gets filled with dynamic buttons and strings using a RecyclerView. Each button and string gets added to the view one at a time, and each time a button and string gets added, I want to send the dynamic string to my button's onClick in RecyclerViewHolder.
I tried to use this question as a guide:
how to send data using bundle inside RecyclerView onclick method
but they have a set list of strings and drawables to send to their RecyclerView.ViewHolder, whereas mine are being created dynamically. Here is where I am at and any help would be appreciated!
RecyclerView.ViewHolder where I want to send my string to onClick. In my log, I want to see the name of the button I am clicking on pop up.
public class RecyclerViewHolders extends RecyclerView.ViewHolder
implements View.OnClickListener
{
public TextView AName;
public ImageButton AButton;
ItemObject itemObject;
List<ItemObject> namelist=HomeFragment.getListdata();
public RecyclerViewHolders(View itemView) {
super(itemView);
AName = (TextView) itemView.findViewById(R.id.new_name);
AButton = (ImageButton) itemView.findViewById(R.id.new_button);
itemView.findViewById(R.id.new_button).setOnClickListener(this);
}
public void onClick(View view) {
String name = namelist.get(getAdapterPosition()).getName();
Log.d("tag_name", "Name of button that was clicked" + name);
}
}
My Activity that initiates the RecyclerView, where I am trying to send the string from. "createButton" method is where drawable and string are first sent to from another method:
public class HomeFragment extends Fragment {
private GridLayoutManager lLayout;
RecyclerViewAdapter rcAdapter;
private static Context mContext;
static String Name;
static Drawable draw;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getContext();
List<ItemObject> myList = new ArrayList<>();
rcAdapter = new RecyclerViewAdapter(getActivity(), myList);
}
// onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment, container, false);
Log.d("tag_name", "Go through onCreateView");
lLayout = new GridLayoutManager(getActivity(), 2,
GridLayoutManager.HORIZONTAL, false);
RecyclerView rView = (RecyclerView) view.findViewById(R.id.recycler_view);
rView.setHasFixedSize(true);
rView.setLayoutManager(lLayout);
rView.setAdapter(rcAdapter);
return view;
}
public void createButton(Drawable d, String aName) {
Name = aName;
draw = d;
Log.d("tag_name", "Make sure createButton is called" + rcAdapter);
rcAdapter.addItem(new ItemObject(aName, d));
}
public static List<ItemObject> getListdata() {
Log.d("tag_name", "List to send to onClickListener" + Name);
List<ItemObject> namelist = new ArrayList<>();
namelist.add(new ItemObject(Name,draw));
return namelist;
}
}
And ItemObject:
public class ItemObject {
private String name;
private Drawable d;
public ItemObject(String name, Drawable d) {
this.name = name;
this.d = d;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Drawable getPhoto() {
return d;
}
public void setPhoto(Drawable d) {
this.d = d;
}
}
My current error, which occurs in RecyclerViewHolders, where I define "String Name":
05-24 14:12:53.350 7295-7295/it.anddev.bradipao.janus W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x437c0160)
05-24 14:12:53.360 7295-7295/it.anddev.bradipao.janus E/AndroidRuntime: FATAL EXCEPTION: main
Process: it.anddev.bradipao.janus, PID: 7295
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at it.anddev.bradipao.janus.RecyclerViewHolders.onClick(RecyclerViewHolders.java:50)
at android.view.View.performClick(View.java:4487)
at android.view.View$PerformClick.run(View.java:18746)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 361
Reputation: 315
I found a link that helped me solve this problem, although my solution is slightly different. For anyone else who might have the same question.
Passing data on click in RecyclerView.Adapter
Reminder of what I wanted to accomplish: "I want to see the name of the button I am clicking on pop up." So, each button click will be unique with the name that I assigned the button.
I realized I can access the position of my click and the string associated with the click in RecyclerViewAdapter and attach my onClickListener to the RecyclerViewHolder. This makes the solution way more simple than what I was trying to do by sending my string list to RecyclerViewHolder.
So, in my RecyclerViewAdapter I created an onClickListener in a custom method:
private class downloadOnClickListener implements View.OnClickListener{
String aName;
public downloadOnClickListener(String file){
this.aName = file;
}
@Override
public void onClick(View v) {
Log.d("tag_name", "Name of button being pressed" + aName);
}
}
And in onBindViewHolder I attached the listener to the RecyclerHolder
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
ItemObject itemObject = itemList.get(position);
holder.itemObject = itemObject;
holder.AButton.setOnClickListener(new downloadOnClickListener(itemList.get(position).getName()));
}
Upvotes: 0
Reputation: 3859
Looks like getAdapterPosition()
is returning value greater than the length of your namelist
(it is just one item). Try to figure this out.
Upvotes: 1
Reputation: 19223
implement OnClickListener
to your Fragment
and pass in constructor to Adapter
and inside to each RecyclerViewHolder
do you know that keeping as static
your Drawable
and Name
results same drawable and name in each fragment you add? (last set)
Upvotes: 1