Reputation: 3
I am beginner in Android so, sorry for my probably noobie question. I've got RecyclerView
list in my application. I created DataAdapter and Activity for that. I also created two layouts: favorite_items(assigned to Data Adapter) and favorite_activity(assigned to Activity). In favorite_items layout(Data Adapter) I've got button "Share" and in Activity I've got function(ButtonLogic). I would like to call function from activity but i don't know how. I must add that RecyclerView
works fine.
ACTIVITY
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:"));
}
});
ADAPTER
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);
}
}
}
Upvotes: 0
Views: 86
Reputation: 29360
Create an interface like this
interface Listener {
private void onItemClicked(String text);
}
and implement it in your Activity. When you construct your adapter, pass this interface
public FavoriteAdapter(Context mCtx, List<Favorite> favoriteList, Listener listener) {
this.mCtx = mCtx;
this.favoriteList= favoriteList;
this.listener = listener
}
Set the listener in your bind method
@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 */);
}
};
}
Upvotes: 1