how to send data from one non activity class to another android - android

In my android app I am using an Adapter inside another Adatper. Let's say child and parent Adapter. In parent Adapter I used onclick method on an item and instantiate the child Adapter. Now onClick on the view in child Adapter I want to send signal to parent Adapter and run a function..
This is my code:
// This is in parent layout
holder.liker.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
holder.reaction_layout.setVisibility(View.VISIBLE);
if(holder.cursor.getCount()>0)
{
holder.show_reaction=true;
holder.tinyDB.putString("post_id",String.valueOf(holder.username.getTag()));
holder.tinyDB.putString("act_id",String.valueOf(holder.post_comment.getTag()));
while(holder.cursor.moveToNext())
{
holder.imageModelArrayList.add(new ReactionModel(holder.cursor.getString(1),holder.cursor.getString(3),holder.cursor.getString(2)));
holder.recyclerView.setAdapter(holder.adapter); //Here i instantiate the child adapter
}
}
return true;
}
});
Now this is click function of child Adapter:
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tinyDB.putString("image",imageModelArrayList.get(position).getImage_drawable());
String react_id=String.valueOf(holder.title.getTag());
String act_id=tinyDB.getString("act_id");
String post_id=tinyDB.getString("post_id");
NetworkController.postLikePojoClassCall(base64,Integer.parseInt(act_id),Integer.parseInt(react_id),Integer.parseInt(post_id)).enqueue(new retrofit2.Callback<PostLikePojoClass>() {
#Override
public void onResponse(Call<PostLikePojoClass> call, retrofit2.Response<PostLikePojoClass> response) {
if(response.isSuccessful()) {
System.out.println("response__ " + response.body().getSuccess());
}else{
System.out.println("response__ " + response.errorBody());
}
}
#Override
public void onFailure(Call<PostLikePojoClass> call, Throwable t) {
System.out.println("failure__ " + t.getMessage());
}
});
}
});
In this click I want to send signal to parent Adapter that item has been clicked.

Use callback method to trigger your parent adapter
Create an interface
public interface MyInterface{
void click();
}
Make parent Adapter implement the interface
class YourParentAdapter extends RecyclerView.Adapter<YourViewHolder> implements MyInterface
Pass your parent adapter into the child adapter through the constructor
public ChildAdapter(MyInterface myInterface){
this.myInterface = interface;
}
Now you can use the function from parent Adapter inside the child Adapter:
myInterface.click();

Related

RecyclerView Adapter onBind method

I have in my MainActivity 3 RecyclerViews .
One of them in a bottom sheet and it is the main one (By Default the Bottom sheet is Open To Display this one ), in its adapter's onbind method I made an onClickListener so that I want when the user clicks on an item in it,
I want to go back to the main activity class to set To Start a method which it's rolled is to close the Bottom Sheet and set the data for the next recycling view (which will appear when the Bottom Sheet is closed)
..... The issue here is how to start this method from the onBind method's Listener and give it a parameter from this viewHolder as its name and some of its attributes
if there is something not clear please let me know
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int position) {
viewHolder.categoryImage.setImageResource(mRowOfCategories.get(position).getCategoryImage());
viewHolder.categoryName.setText(mRowOfCategories.get(position).getCategoryName());
viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
Easiest:
You declare the method you want to call in your Activity. It has to be public:
public void method(){}
Then, in the Constructor of the Adapter, you pass a reference to the Activity as a parameter:
public MyAdapter(Activity activity){}
And, in the onBindViewHolder:
MyActivity mActivity=(MyActivity)activity;
mActivity.method();
You can do that easily.
Define custom interface
public Interface CustomEventListener
{
public void MyEventListener(String message); //you can change parameters
}
In your adapter class
public Adapter ......... {
private CustomEventListener listener;
public void setListener(CustomEventListener listener)
{
this.listener = listener;
}
//Your onBind
Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int position) {
viewHolder.categoryImage.setImageResource(mRowOfCategories.get(position).getCategoryImage());
viewHolder.categoryName.setText(mRowOfCategories.get(position).getCategoryName());
viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null)
listener.MyEventListener("Message");
}
});
}
}
In your Activity when create Adapter add this code
public void InitAdapter()
{
yourAdapter = new Adapter(); // bloa bla bla
yourAdapter.setListener(new CustomEventListener() {
public void MyEventListener(String message)
{
// then do what you want
}
}
}
You need to use Listener for handling the click in your Adapter something like this:
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
then call the listener with:
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int position) {
...
viewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call the listener.
mListener.onItemClick(v, viewHolder.getAdapterPosition());
}
});
}
then when you're using the adapter, set the listener with something like this:
adapter.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(View view, int position) {
// do something with the view and position.
}
});
You want your recycleviews to be aware of each other. You'll need a class, maybe MainActivity, that will keep the selected value, also clear it upon request, in between recycleviews. Then you'll need that value to propagate the next recycleview. If you want to be efficient then you'll use one recycleview and swap the data in between selections, and animate it so it looks like a new recycleview is created.

How to pass data one textview to another textview inside activity

I am trying to show data in toolbar. I have recyclerview where I set movies name. when I click any movie name, the name show in toolbar. I defined the onclick method in adapter class.
This is adapter class
Make public method in adapter class and call that method in onClick
ExampleAdapter.java
holder.tvMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onSelectMovie(list.get(holder.getAdapterPosition()).getname());
}
}
});
public void onSelectMovie(String movieName)
{
}
Override onSelectMovie method where you call the adapter constructor
exampleAdapter = new ExampleAdapter() {
#Override
public void onSelectMovie(String movieName) {
super.onSelectImage(movieName);
setTitle(movieName);
}
};

how to access items in RecyclerView in fragment from an activity

I have an activity that has a TabLayout(where my Fragments are in)
and myFragment has a RecyclerView itself.
I want to show every touched Item's name in a textView in myActicvity.
This is an image that explains my problem.
How should I do that? Thanks for any Help!.
You can use Event bus: https://github.com/greenrobot/EventBus. This library help you to send events from one activity/fragment/view to another. You must just add onclicklistener for item root element, and do something like this: EventBus.getDefault().post(getItemName());. And at your activty add this:
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(String name) {
textView.setText(name);
};
And don't forget to register and unregister listener.
you should write onItemCLickListener in your recyclerView adapter , and then call it from your activity to set textview text.
Adapter Code:
OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onClick(int position,String itemName);
}
#Override
public void onBindItemViewHolder(final AdapterRecycelerView.ViewHolderRecyclerItem holder, final int position) {
try {
holder.linearItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (onItemClickListener != null)
onItemClickListener.onClick(position,arrayList.get(position).getName());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
Activity Code:
adapterRecycelerView.setOnItemClickListener(new AdapterRecycelerView.OnItemClickListener() {
#Override
public void onClick(int position,String itemName) {
textview.setText(itemName);
}
});

Recycler View onClick

I have an activity that creates a tab layout inside of it. The layout contains two fragments. The first fragment has a recycler view. Each card in the recycler view has a check box and a string.
If I click on the checkbox I want to send this card's string to a List located in the activity so I can populate it onto the second tab fragment.
On the first fragment cards I have an on click that sets boolean to true which is saved on an object. I am trying to figure out how to grab this object when it is clicked and send it to the activities list.
Activity-
public class MainActivity extends AppCompatActivity {
public static List toSendList = new ArrayList();
...more code
}
Recycler Adapter-
//initialize variables...
CheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (CheckBox.isChecked()) {
//MainActivity.toSendList.add(Obj);
obj.setIsChecked(true);
}
}
});
... more code
You might try EventBus and see if you like it. With it, you define an
'event' which can also be the object that you want to pass to your activity (or an event which wraps it). In the receiving activities, add your Subcribers. You then pass events/objects by calling
EventBus.getDefault().post(new CustomEvent());
and any place where a Subcriber is still attached will receive the event.
You can implement a listener which you will use on the Activity. For example:
You create a interface for a click listener.
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.PhotoHolder> {
// ... all the code
public interface RecyclerViewOnClickListener(/* same arguments as above */);
}
On your view holder.
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ViewHolder(View v) {
super(v);
// ... do all the initialization
v.setOnClickListener(this);
}
#Override
public void onClick(View v) {
mOnClickListener(int position /* or whatever argument you like */)
}
}
On your RecyclerAdapter you store a listener for the click. So the adapter would end up with more code:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.PhotoHolder> {
// ... all the code
public interface RecyclerViewOnClickListener(/* same arguments as above */);
public RecyclerAdapter(RecyclerViewOnClickListener onClickListener) {
mOnClickListener = onClickListener;
}
}
So finally, in your Activity you just instantiate the RecyclerViewOnClickListener and pass it as an argument for the adapter.
try this :
Activity-
public class MainActivity extends AppCompatActivity {
public static List toSendList = new ArrayList();
...more code
public setPassData(String string)
{
//do something with this string
}
}
Fragment A/B
public void setPassData(String string)
{
((ActivityName)getActivity()).setPassData(string);
}
Recycler Adapter-
//initialize variables...
CheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (CheckBox.isChecked()) {
//MainActivity.toSendList.add(Obj);
obj.setIsChecked(true);
((FragmentName)mContext).setPassData(SomeStringToPass);
}
}
});

how to call button on clickListner outside the custom adapter of listview.

I have a Button on Listview and a custom adapter that loads the ListView. I want to call the Button onClickListner in main class not in the adapter class. how can i do this.
You can create one Global Interface
Like this
package com.radiofrance.interfaces;
public abstract class GlobalInterface implements eventInterface {
public void onClick(int values) {
}
public void onCancel() {
}
}
Then Implement this interface in Your Activity from which you call your adapter
public class SampleClickListener extends GlobalInterface {
#Override
public void onClick(final int values, final boolean state) {
}
}
now in your adapter pass this object of interface
Adapter adt = new Adpater(this, R.layout.row, arrRow,
new SampleClickListener());
and from your adapter call
listner.onClick(position, false);
Thanks

Categories

Resources