I want to start new activity and also passing JSON data to the new activity when i click an item on the recyclerview. i followed a video in youtube: https://www.youtube.com/watch?v=OfsiccfUWVc&index=6&list=PLaoF-xhnnrRW_FGeacuT1VLqnRMKfpp4v but the video didn't show how to go to new activity once i clicked the item on recyclerview. from the video, i wanted to click on the item and then new activity will bring me to the 'more detail page' about the item.
i have tried to do the intent but i am not sure how to call from the new main activity
this is my adapter code:
public class LonghouseAdapter extends RecyclerView.Adapter<LonghouseViewHolder> {
Context context;
List<LonghousesCategoryOne> longhousesCategoryOneList;
List<Category> categories;
private OnItemClickListener mlistener;
public LonghouseAdapter(Context context, List<LonghousesCategoryOne> longhousesCategoryOneList) {
this.context = context;
this.longhousesCategoryOneList = longhousesCategoryOneList;
}
#NonNull
#Override
public LonghouseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.longhouse_item_layout,null);
return new LonghouseViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull LonghouseViewHolder holder, final int position) {
holder.txt_price.setText(new StringBuilder("").append(longhousesCategoryOneList.get(position).Price));
holder.txt_longhouse_name.setText(new StringBuilder("").append(longhousesCategoryOneList.get(position).Name));
Picasso.with(context)
.load(longhousesCategoryOneList.get(position).Link)
.into(holder.img_longhouse);
holder.setItemClickListener(new IItemClickListener() {
#Override
public void onClick(View v) {
Common.currentCategory = categories.get(position);
context.startActivity(new Intent(context, descriptionsActivity.class));
}
});
}
#Override
public int getItemCount() {
return longhousesCategoryOneList.size();
}
}
this is my viewholder code:
public class LonghouseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView img_longhouse;
TextView txt_longhouse_name, txt_price;
public void setItemClickListener(IItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
IItemClickListener itemClickListener;
public LonghouseViewHolder(View itemView) {
super(itemView);
img_longhouse = (ImageView) itemView.findViewById(R.id.image_longhouse);
txt_longhouse_name = (TextView) itemView.findViewById(R.id.txt_longhouse_name);
txt_price = (TextView) itemView.findViewById(R.id.txt_longhouse_price);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
itemClickListener.onClick(v);
}
}
in my descriptionActivity, how do i set the item to my xml view?so far i have done this.
public class descriptionsActivity extends AppCompatActivity {
retreatSecondApi mservice;
ImageView img_longhouse;
TextView txt_longhouse_description, txt_price;
Context context;
List<LonghousesCategoryOne> longhousesCategoryOneList;
CompositeDisposable compositeDisposable = new CompositeDisposable();
#Override
protected void onCreate(Bundle savedInstanceState) {
mservice = Common.getAPI();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_descriptions);
img_longhouse = (ImageView)findViewById(R.id.placeImageView);
txt_longhouse_description = (TextView)findViewById(R.id.placeDescTextView);
txt_price = (TextView)findViewById(R.id.txt_longhouse_price);
txt_longhouse_description.setText()
}
}
Can anyone guide me on this? Thank you
As #rgaraisayev recommends in your onClick() listener uncomment the call to startActivity() i.e.
holder.setItemClickListener(new IItemClickListener() {
#Override
public void onClick(View v) {
Common.currentCategory = categories.get(position);
context.startActivity(new Intent(context, descriptionsActivity.class));
}
});
solved it, so in my holder view:
holder.setItemClickListener(new IItemClickListener() {
#Override
public void onClick(View v) {
Common.longhousedesc = longhousesCategoryOneList.get(position);
context.startActivity(new Intent(context, descriptionsActivity.class));
}
});
and in my new activity, i bind the data to my xml view like this:
txt_longhouse_description.setText(Common.longhousedesc.descriptions);
Related
public class MainActivity extends Activity implements
GetDataContract.View,RecyclerItemClickListener {
private Presenter mPresenter;
RecyclerView recyclerView;
LinearLayoutManager linearLayoutManager;
CountryAdapter countryAdapter;
EditText etEnterName;
Button btAddItem;
List<CountryRes> allCountriesEditValue;
List<CountryRes> allCountriesData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getView();
}
private void getView() {
/*presentator */
mPresenter = new Presenter(this);
/*initiliaze of id*/
recyclerView = (RecyclerView) findViewById(R.id.recycler);
etEnterName = (EditText) findViewById(R.id.etEnterName);
btAddItem = (Button) findViewById(R.id.btAddItem);
linearLayoutManager = new LinearLayoutManager(this);
/*initiliaze the arraylist*/
allCountriesData=new ArrayList<>();
recyclerView.setLayoutManager(linearLayoutManager);
btAddItem.setOnClickListener(new AddButtonClick());
}
#Override
public void onGetDataFailure(String message) {
Log.d("Status", message);
}
#Override
public void onGetDataSuccess(String message, List<CountryRes>
allCountriesData) {
/*add the value mannulay*/
CountryRes countryRes = new CountryRes();
countryRes.setName(etEnterName.getText().toString());
allCountriesData.add(countryRes);
countryAdapter = new CountryAdapter(getApplicationContext(),
allCountriesData, (RecyclerItemClickListener) this);
recyclerView.setAdapter(countryAdapter);
/*set the data in the room*/
AppDataBase database = AppDataBase.getAppDatabase(this);
DataGenerator.with(database).generateCats(allCountriesData);
Logger.displayCatsInLog(database.catDao().loadAll());
countryAdapter.notifyDataSetChanged();
}
/*on item click*/
#Override
public void onDashBoardItemClick(String pos) {
Bundle bundle = new Bundle();
bundle.putString("TABVALUE", pos);
Intent intent = new Intent(getApplicationContext(),
DynamicTabsActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
private class AddButtonClick implements View.OnClickListener {
#Override
public void onClick(View view) {
mPresenter.getDataFromURL(getApplicationContext(), "");
}
}
}
class adapter
public class CountryAdapter extends
RecyclerView.Adapter<CountryAdapter.MyViewHolder> {
private Context context;
private List<CountryRes> list = new ArrayList<>();
private List<CountryRes> list_edit = new ArrayList<>();
private RecyclerItemClickListener onRecyclerItemClickListener;
public CountryAdapter(Context context, List<CountryRes> list,
RecyclerItemClickListener onRecyclerItemClickListener) {
this.context = context;
this.list = list;
this.onRecyclerItemClickListener = onRecyclerItemClickListener;
}
#Override
public CountryAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View layoutView;
layoutView =
LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent,
false);
return new FooterViewHolder(layoutView,
onRecyclerItemClickListener);
}
#Override
public void onBindViewHolder(CountryAdapter.MyViewHolder holder, int
position) {
holder.tvCountryName.setText(list.get(position).getName());
}
#Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvCountryName;
LinearLayout llListItem;
public MyViewHolder(View itemView) {
super(itemView);
tvCountryName = (TextView)
itemView.findViewById(R.id.tv_country_name);
llListItem = (LinearLayout)
itemView.findViewById(R.id.llListItem);
}
}
private class FooterViewHolder extends MyViewHolder {
public FooterViewHolder(View layoutView, final
RecyclerItemClickListener onRecyclerItemClickListener) {
super(layoutView);
llListItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (onRecyclerItemClickListener != null) {
onRecyclerItemClickListener.onDashBoardItemClick(tvCountryName.getText().toS
tring());
}
}
});
}
}
}
Interface
public interface RecyclerItemClickListener {
void onDashBoardItemClick(String pos);
}
Case 1: Inside ViewHolder if you need to understand exactly position of clicked item you could call getAdapterPosition().
Case 2: You just need to receive all items of adapter. It is simple, make getter method which will return the items.
Case 3: Send info to activity, first make some specific event listener of your view inside ViewHolder, then event triggers (user clicks or other) and you call your interface method which you passed inside adapter, that was implemented in activity.
Tell me if you need an example, i will do it.
Try this one,
Button button= (Button) findViewById(R.id.your_button_id);
button.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
//create new ArrayList.
//ArrayList<CountryRes> showList=new ArrayList<>;
//now, put all stored data of a list in this arraylist.
showList.addAll(allCountriesData);
}
});
Hello guys, someone can help me to finish this simple project, please?
I don't know how to configure the click on the items for opening a new activities.
RecycleViewAdapter.java
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<ModelData> users;
private Context context;
public RecyclerViewAdapter(List<ModelData> users, Context context) {
this.users = users;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.layout_row, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder( ViewHolder holder, int position) {
ModelData user = users.get(position);
holder.nomeUser.setText(user.getName());
holder.userImage.setImageResource(user.getUserImage());
}
#Override
public int getItemCount() {
return users.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView nomeUser;
private ImageView userImage;
public ViewHolder( View itemView) {
super(itemView);
nomeUser = itemView.findViewById(R.id.nomeText);
userImage = itemView.findViewById(R.id.imageView);
context = itemView.getContext();
}
}
ModelData.java
package com.example.aicopolilattico.splashscreen;
import java.security.PublicKey;
public class ModelData {
private String name;
private int userImage;
public ModelData() {}
public ModelData(String name, int userImage ) {
this.name = name;
this.userImage = userImage;
}
public String getName() {
return name;
}
public int getUserImage() {
return userImage;
}
ActivityMain.java
package com.example.aicopolilattico.splashscreen;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ActivityMan extends AppCompatActivity {
private RecyclerView recycleView;
private List<ModelData> users;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityone);
recycleView = findViewById(R.id.recycleView);
users = new ArrayList<>();
users.add(new ModelData("Jack and Jones", R.drawable.jackcjones111));
users.add(new ModelData("Marlboro", R.drawable.marlb));
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recycleView.setLayoutManager(linearLayoutManager);
recycleView.setAdapter(new RecyclerViewAdapter(users, this));
recycleView.setHasFixedSize(true);
}
Guys is really important.
Thanks in advance to have patience and time to give me. :)
[ ! ! ! UPDATES ! ! ! ]
I've tried to write the code below, but when I click on the item, it pick up me on the first activity of the app. Advices? Thank a lot
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>
{
private List<ModelData> users;
private Context context;
public RecyclerViewAdapter(List<ModelData> users, Context context)
{
this.users = users;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.layout_row, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder( ViewHolder holder, int position)
{
final ModelData user = users.get(position);
holder.nomeUser.setText(user.getName());
holder.userImage.setImageResource(user.getUserImage());
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,Photo_Man.class);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount()
{
return users.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView nomeUser;
private ImageView userImage;
public ViewHolder( View itemView)
{
super(itemView);
nomeUser = itemView.findViewById(R.id.nomeText);
userImage = itemView.findViewById(R.id.imageView);
context = itemView.getContext();
}
}
}
can u replace click listener like below code
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
context.startActivity(new Intent(context, Photo_Man.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
});
The best and easiest way to achieve that is sending a listener of that event from the activity to the recyclerview.
Something like this...
Create an interface for listening click events:
public interface OnItemClickListener {
void onItemClick();
}
Modify the adapter to receive the listener:
private OnItemClickListener listener;
public RecyclerViewAdapter(List<ModelData> users, Context context, OnItemClickListener listener ) {
this.users = users;
this.context = context;
this.listener = listener;
}
Create listener from activity (From here you can open new activities):
OnItemClickListener listener = new OnItemClickListener() {
#Override public void onItemClick() {
Intent intent = new Intent(ActivityMain.this, NewActivity.class);
startActivity(intent);
}
};
Send that interface to the adapter:
recycleView.setAdapter(new RecyclerViewAdapter(users, this, listener));
Set this listener to the views you want to observe the event:
#Override
public void onBindViewHolder( ViewHolder holder, int position) {
ModelData user = users.get(position);
holder.nomeUser.setText(user.getName());
holder.userImage.setImageResource(user.getUserImage());
holder.userImage.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick();
}
});
}
First go to you recyclerview adapter's onBindViewHolder method.
Then simply add below code. here itemView is the particular one item of recyclerview
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
// here you go for second activity using intent
Intent intent = new Intent(context, SecondActivity.class); // here you need context from constructor, SecondActivity is another activity
context.startActivity(intent); // this will call the SecondActivity intent
((Activity)context).finish(); // this is for just finish current activity (OPTIONAL, as per requirement)
}
});
if you want to goto activity
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(context,SomeActivity.class);
context.startActivity(intent);
}
});
if you want to goto fragment
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
FragmentManager manager = ((Activity) context).getFragmentManager();
manager.beginTransaction().replace(R.id.transaction,get_posts).addToBackStack(null).commit();
}
});
i have a recyclerview and i want to when click on it receive the position and one String and sent to other activity and run it
i dont know where this code has problem.
Mainactivity
#Override
public void onItemClick(int i) {
datamodel clickedItem = postlist.get(i);
Intent detailIntent = new Intent(MainActivity.this, Main2Activity.class);
detailIntent.putExtra(EXTRA_URL, clickedItem.getmTitle());
startActivity(detailIntent);
}
Adapter codes:
public class rrecyclerviewadapter extends RecyclerView.Adapter<rrecyclerviewadapter.myholder> {
private OnItemClickListener mListener;
private Context context;
private List<datamodel>mylist;
public interface OnItemClickListener {
void onItemClick(int i);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener;
}
public rrecyclerviewadapter(Context context, List<datamodel>mylist){
this.context=context;
this.mylist=mylist;
}
#NonNull
#Override
public myholder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(context).inflate(R.layout.each_item,viewGroup,false);
return new myholder(view);
}
#Override
public void onBindViewHolder(#NonNull myholder myholder, int i) {
datamodel datamodel=mylist.get(i);
myholder.eachitemtxt.setText(datamodel.getmTitle());
Picasso.with(context).load(datamodel.getMimageurl()).placeholder(R.drawable.pic3).error(R.drawable.pic4).into(myholder.eachitemimg);
}
#Override
public int getItemCount() {
return mylist.size();
}
public class myholder extends RecyclerView.ViewHolder {
private TextView eachitemtxt;
private ImageView eachitemimg;
public myholder(#NonNull final View itemView) {
super(itemView);
eachitemimg = (ImageView) itemView.findViewById(R.id.imageview_eachitem);
eachitemtxt = (TextView) itemView.findViewById(R.id.textview_eachitem);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "id:" + getAdapterPosition(),
Toast.LENGTH_SHORT).show();
int i=getAdapterPosition();
if (i != RecyclerView.NO_POSITION) {
mListener.onItemClick(i);
}}
});}}}
here there is full adapter code
on other activity i get intent.
in the adapter when testing Toast its completely works but on the mlistener and
setting id it does not work
found it.
just need to add
Adapter.setOnItemClickListener(Activitymain.this)
to your Mainactivity
i just changed your holder class in adapter try the below code
public class myholder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView eachitemtxt;
private ImageView eachitemimg;
public myholder(#NonNull final View itemView) {
super(itemView);
eachitemimg = (ImageView) itemView.findViewById(R.id.imageview_eachitem);
eachitemtxt = (TextView) itemView.findViewById(R.id.textview_eachitem);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int p = getAdapterPosition();
datamodel clickedItem = postlist.get(p);
Intent detailIntent = new Intent(context, Main2Activity.class);
detailIntent.putExtra(EXTRA_URL, clickedItem.getmTitle());
context.startActivity(detailIntent);
}
}
I have this method in my activity (CreatePhotostoryActivity):
#Override
public void showEditField(Moment oldMoment, final int index){
listRecycler.setVisibility(View.GONE);
editCaption.setVisibility(View.VISIBLE);
saveCancelNavbar.setVisibility(View.VISIBLE);
momentView.setVisibility(View.GONE);
photoStoryNavbar.setVisibility(View.GONE);
header.setVisibility(View.GONE);
Picasso.with(getApplicationContext()).load(oldMoment.photoUri).into(displayPhoto);
imageCaption.setText("");
imageCaption.setText(oldMoment.caption);
backIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
closeEditMoment();
}
});
saveText.setText("Save Moment");
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateMoment(imageCaption.getText().toString(), index);
Intent i = new Intent(CreatePhotostoryActivity.this, RVAdapter.class);
i.putExtra("isSaved", true);
startActivity(i);
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
closeEditMoment();
Intent i = new Intent(CreatePhotostoryActivity.this, RVAdapter.class);
i.putExtra("isSaved", false);
startActivity(i);
}
});
}
In my RecyclerView adapter, showEditField is called using an interface, and it does work except that I am unable to detect whether saveButton or cancelButton was clicked. This is how it's called in the onBindViewHolder method of my RecyclerView adapter (RVAdapter):
((RecyclerView.ViewHolder) momentViewHolder).itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mAdapterCallback.showEditField(moments.get(position), position);
Intent intent = ((CreatePhotostoryActivity) mContext).getIntent();
Boolean isSaved = intent.getExtras().getBoolean("isSaved");
if (isSaved) {
holder.momentCaption.setText(moments.get(position).caption);
holder.momentPlaceholder.setVisibility(View.GONE);
holder.momentCaption.setVisibility(View.VISIBLE);
} else {
holder.momentPlaceholder.setVisibility(View.VISIBLE);
holder.momentCaption.setVisibility(View.GONE);
}
notifyDataSetChanged();
});
Do note my attempt of using an intent to find out which button was clicked. It doesn't work. momentCaption remains gone and momentPlaceHolder remains visible even though saveButton was clicked. The app also crashes afterwards.
Edit: I think calling this mAdapterCallback.showEditField(moments.get(position), position); before setting the values of momentCaption and momentPlaceholder is problematic, since showEditField calls other methods (e.g. closeEditMoment() before the intent is retrieved from the adapter.
Create an interface with two methods like saveBtnClick() and cancelBtnClick().
Implement the interface in your Activity.
Implement Click listener for both the buttons in the ViewHolder of your RecyclerView and call the Appropriate methods using Inteface's Object.
//Interface
public interface ManageButtonClicks
{
void saveBtnClick();
}
//Activity
public class MainActivity extends AppCompactActivity implements ManageButtonClicks
{
ManageButtonClicks manageButtonClicks=this;
MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
//Do the operations
adapter = new MyAdapter(this,adapterList,manageButtonClicks);
recyclerView.setAdapter(adapter);
}
void saveBtnClick()
{
Toast.makeText(this,"Clicked",Toast.LENGTH_SHORT).show();
}
}
//Adapter Class
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
{
ManageButtonClicks manageButtonClicks;
public MyAdapter(Context context, List< ModelClass > data, ManageButtonClicks manageButtonClicks)
{
inflater = LayoutInflater.from(context);
this.mDataList = data;
this.ctx = context;
util = new Util(ctx);
this.manageButtonClicks = manageButtonClicks;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
ViewGroup nonPrimeRow = (ViewGroup) inflater.inflate(R.layout.rowlayout, parent, false);
MyViewHolder_NON_PRIME holderNonPrime = new MyViewHolder_NON_PRIME(nonPrimeRow);
return holderNonPrime;
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position)
{
ModelClass current = mDataList.get(position);
MyViewHolder_NON_PRIME holder_not_prime = (MyViewHolder_NON_PRIME) holder;
holder_not_prime.setdata(current);
}
class MyViewHolder extends RecyclerView.ViewHolder
{
public MyViewHolder(View itemView)
{
super(itemView);
}
}
public class MyViewHolder_NON_PRIME extends MyViewHolder
{
private Button btnSave;
public MyViewHolder_NON_PRIME(View view)
{
super(view);
btnSave = (MonteButton) view.findViewById(R.id.btnSave);
}
public void setdata(final ModelClass current)
{
btnSave.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
manageButtonClicks.saveBtnClick();
}
});
}
}
You have a global variable inside recyclerView adapter:
RecyclerViewAdapter {
private boolean isSaveButtonClicked = false;
private boolean isCancelButtonClicked = false;
public void saveButtonClicked(){
isSaveButtonClicked = true;
}}
public void cancelButtonClicked(){
isCancelButtonClicked = true;
}}
When your activity call onclickListeners , you will notice your adapter.
CreatePhotostoryActivity(){
private RecyclerView adapter;
onSaveButtonClicked(){
adapter.saveButtonClicked();
}
onCancelButtonClicked(){
adapter.cancelButtonClicked();
}
}
Is any way to transfer the current list of items to a new Activity using OnClickListener of ViewHolder? Or any other way?
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.EventsViewHolder> {
List<Event> events; //get this List
RVAdapter(List<Event> events) {
this.events = events;
}
public static class EventsViewHolder extends RecyclerView.ViewHolder {
TextView date;
TextView text;
ImageView photo;
EventsViewHolder(final View itemView) {
super(itemView);
date = (TextView) itemView.findViewById(R.id.date);
text = (TextView) itemView.findViewById(R.id.text);
photo = (ImageView) itemView.findViewById(R.id.photo);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
SendObject sendObject = new SendObject(setList); //and transfer it here
Intent intent = new Intent(context, ReadEvent.class);
Bundle bundle = new Bundle();
bundle.putSerializable("events", sendObject);
bundle.putInt("pos", getAdapterPosition());
bundle.putString("test", "test");
intent.putExtras(bundle);
context.startActivity(intent);
}
});
}
}
Solution 1: Create method in ViewHolder in which you pass your list
Solution 2: Define your ViewHolder as not static class and pass your list
Another approach:
// Solution 1:
public class EventsViewHolder extends RecyclerView.ViewHolder {
EventsViewHolder(final View itemView) {
super(itemView);
}
public void bind(List<Event> events) {
...
SendObject sendObject = new SendObject(events); //and transfer it here
...
}
}
// Solution 2:
public class EventsViewHolder extends RecyclerView.ViewHolder {
EventsViewHolder(final View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SendObject sendObject = new SendObject(events); //and transfer it here
}
});
}
}
Adapter Class.
public ServicesHomeAdapter(Context context, ArrayList<HomeScreenData> homeScreenDataArrayList,OnItemClickedListener listener) {
this.context=context;
this.homeScreenDataArrayList=homeScreenDataArrayList;
this.listener=listener;
}
#Override
public ServicesHomeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_service,parent,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(final ServicesHomeAdapter.ViewHolder holder, int position) {
final HomeScreenData homeScreenData = homeScreenDataArrayList.get(position);
}
#Override
public int getItemCount()
{
return (null!=homeScreenDataArrayList?homeScreenDataArrayList.size():0);
}
public class ViewHolder extends RecyclerView.ViewHolder {
protected ImageView ivSection;
protected TextView txImageName;
public ViewHolder(View itemView)
{
super(itemView);
this.ivSection= (ImageView) itemView.findViewById(R.id.ivcard);
this.txImageName= (TextView) itemView.findViewById(R.id.txtimgname);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClicked(getAdapterPosition());
}
});
}
}
public interface OnItemClickedListener{
void onItemClicked(int position);
}
}
Implement the interface in your Activity.
#Override
public void onItemClicked(int position) {
Intent intent = new Intent(getActivity(), A.class);
Pojo Class pojo = arrayList.get(position);
intent.putExtra(TAG, pojo);
startActivity(intent);
}`
Your Pojo should parseable or serialised but now android recommends to implement parseable then you can pass any Pojo through intent, How you can make your class parseable check this link.
https://developer.android.com/reference/android/os/Parcelable.html
intent.putExtra(key, Serializable/Parcelable) and then in second activity getParcelableExtra(key)/getSerializableExtra(key)
I had a Serializable object already:
public class SendObject implements Serializable{
private List<Event> events;
public SendObject(List<Event> events) {
this.events = events;
}
public List<Event> getEvents() {
return events;
}
}
The main problem was to receive current Adapter List. The problem is solved by using Singleton. The solution was simple.
public class RVAdapter extends RecyclerView.Adapter {
private static RVAdapter rvAdapter;
private List<Event> events;
public RVAdapter() {
}
public static RVAdapter getInstance() {
if (rvAdapter==null) {
rvAdapter = new RVAdapter();
}
return rvAdapter;
}
public void setList(List<Event> events) {
this.events = events;
}
public static class EventsViewHolder extends RecyclerView.ViewHolder {
TextView date;
TextView text;
ImageView photo;
EventsViewHolder(final View itemView) {
super(itemView);
date = (TextView) itemView.findViewById(R.id.date);
text = (TextView) itemView.findViewById(R.id.text);
photo = (ImageView) itemView.findViewById(R.id.photo);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
SendObject sendObject = null;
sendObject = new SendObject(RVAdapter.getInstance().events); //current List
Intent intent = new Intent(context, ReadEvent.class);
Bundle bundle = new Bundle();
bundle.putSerializable("events", sendObject);
bundle.putInt("pos", getAdapterPosition());
intent.putExtras(bundle);
context.startActivity(intent);
}
});
}
}
Anyway, thanks for your responses!