How to getting the current value when i clicked my recycler view - android

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDatasetname;
private Integer[] mexp;
Context context;
public ToggleButton select;
Integer selectedcountint=0;
private Bitmap[] mpro;
private String[] mloc;
private String[] mobj;
private String[] mselected;
public ArrayList<String> nselected = new ArrayList<>();
public static class MyViewHolder extends RecyclerView.ViewHolder{
public CardView mCardView;
public TextView mTextView;
public TextView texp;
public Button pdf;
public ToggleButton select;
public ImageView pro;
public TextView loc;
public MyViewHolder(View v){
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
texp = (TextView) v.findViewById(R.id.setexp);
pdf = (Button) v.findViewById(R.id.moreinfo);
select = (ToggleButton) v.findViewById(R.id.select);
pro = (ImageView) v.findViewById(R.id.setpropic);
loc = (TextView) v.findViewById(R.id.setlocation);
}
}
public MyAdapter(String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){
mDatasetname = myDataset;
mexp=exp;
mpro=pro;
mloc=loc;
mobj=obj;
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
context=parent.getContext();
View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
MyViewHolder vh = new MyViewHolder(vs);
return vh;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position){
holder.mTextView.setText(mDatasetname[position]);
holder.texp.setText(String.valueOf(mexp[position])+" yrs");
holder.pro.setImageBitmap(mpro[position]);
holder.loc.setText(mloc[position]);
holder.select.setText("rejected");
holder.pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent();
intent = new Intent(context, PdfViewer.class);
intent.putExtra("obj",mobj[position]);
context.startActivity(intent);
}
});
holder.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked==true) {
holder.select.setChecked(true);
holder.select.setText("selected");
nselected.remove(mobj[position]+",rejected");
nselected.add(mobj[position]+",selected");
Log.d("dei", String.valueOf(nselected));
selectedcountint=selectedcountint+1;
Intent intent = new Intent("custom-message");
// intent.putExtra("quantity",Integer.parseInt(quantity.getText().toString()));
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)nselected);
intent.putExtra("BUNDLE",args);
intent.putExtra("totalval",selectedcountint);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
} else {
holder.select.setChecked(false);
holder.select.setText("rejected");
nselected.remove(mobj[position]+",selected");
nselected.add(mobj[position]+",rejected");
Log.d("dei", String.valueOf(nselected));
selectedcountint=selectedcountint-1;
Intent intent = new Intent("custom-message");
// intent.putExtra("quantity",Integer.parseInt(quantity.getText().toString()));
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)nselected);
intent.putExtra("BUNDLE",args);
intent.putExtra("totalval",selectedcountint);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
}
});
}
#Override
public int getItemCount() { return mDatasetname.length; }
}
I have a recycler view in my app. I am having a problem. There is an item so that when I click on the item a the funtion in the click b is executed. It would be very great when you clean up the below code for me because I am new to programming, so that I won't be able to solve the problem.

if(getAdapterPosition() != RecyclerView.NO_POSITION) {
...
mobj[getAdapterPosition()]
...
}

It's better if you set your data and listeners in the ViewHolder. You should create a method in your ViewHolder class and pass there item from your list as a parameter. In this method you copy all your code for setting text and listeners. Then in onBindViewHolder you call this method.
First create a class that will be our Adapter item (you should change the names of the fields because I don't know what should they represent, this is just an example):
class AdapterItem {
private String firstString;
private Integer integerValue;
private Bitmap bitmap;
private String secondString;
private String thirdString;
// create also getters, setters for fields
}
Your adapters constructor will look something like this:
public MyAdapter(ArrayList<AdapterItem> data){
this.data = data;
}
And the onBindViewHolder method:
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position){
holder.bind(data[position]);
}
In the ViewHolder class:
void bind(AdapterItem item) {
mTextView.setText(item.getFirstString());
pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, PdfViewer.class);
intent.putExtra("obj", item.getThirdString());
v.getContext().startActivity(intent);
}
});
}

As correctly pointed out by #andrei The code to get the correct string from the obj[] array should be as shown:
public MyAdapter(Context context, String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){
mContext = context; // make mContext as a variable like others
mDatasetname = myDataset;
mexp=exp;
mpro=pro;
mloc=loc;
mobj=obj;
}
holder.pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent();
intent = new Intent(context, PdfViewer.class);
intent.putExtra("obj",mobj[holder.getAdapterPosition()]);
mContext.startActivity(intent);
}
});
And similarly replace all occurrences of position with holder.getAdapterPosition() because holder.getAdapterPosition() will always give you the updated position for an item of recycler view in your code as if you update or delete items without using notifyDataSetChanged() method then the onBindViewHolder will not be called again hence the position will become inconsistent. For detailed information you can refer to this answer.

public class MyAdapter extends RecyclerView.Adapter {
private String[] mDatasetname;
private Integer[] mexp;
private Context context;
public ToggleButton select;
Integer selectedcountint=0;
private Bitmap[] mpro;
private String[] mloc;
private String[] mobj;
public Button pdf;Context m;private String[] mselected;
public ArrayList<String> nselected = new ArrayList<>();
public static class MyViewHolder extends RecyclerView.ViewHolder{
public CardView mCardView;
public TextView mTextView;
public TextView texp;
public Button pdf;
public ToggleButton select;
public ImageView pro;
public MainActivity activity;
public Context ipaset;
public TextView loc;
public View layout;
View forresumeview;
String forresume;
private RecyclerView.ViewHolder s;
public MyViewHolder(final View v) {
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
texp = (TextView) v.findViewById(R.id.setexp);
pdf = (Button) v.findViewById(R.id.moreinfo);
select = (ToggleButton) v.findViewById(R.id.select);
pro = (ImageView) v.findViewById(R.id.setpropic);
loc = (TextView) v.findViewById(R.id.setlocation);
forresumeview=v;
}
void bind(String name, Integer exp, String location, final Bitmap image, final String obj ){
mTextView.setText(name);
texp.setText(String.valueOf(exp) + " yrs");
pro.setImageBitmap(image);
loc.setText(location);
}
public void doonce(final String s){
pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View vs) {
Intent intent = new Intent(forresumeview.getContext(), PdfViewer.class);
intent.putExtra("obj", s);
forresumeview.getContext().startActivity(intent);
// Log.d("sdasd", obj);
}
});
}
}
public MyAdapter(String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){
mDatasetname = myDataset;
mexp=exp;
mpro=pro;
mloc=loc;
mobj=obj;
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
context=parent.getContext();
View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
MyViewHolder vh = new MyViewHolder(vs);
return vh;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position){
holder.bind(mDatasetname[position],mexp[position],mloc[position],mpro[position],mobj[position]);
holder.doonce(mobj[position]);
}
#Override
public int getItemCount() { return mDatasetname.length; }
}
this is the answer ,,,, it works i tried it !!!! Happy coding ......

Related

Android: Pass text from Textview when clicking an Image button inside recycler view

So I have a recycler view with 2 texts and 1 image button. I want to click the image button and then open a new activity and transfering the text from the textViewADV1 to the next activity
My items .xml in my recyclerview consist of the following
<ImageButton
android:id="#+id/imageButton1"
android:clickable="true"
android:onClick="openActivity2"/>
<TextView
android:id="#+id/textViewADV1"
android:text="Line 1"/>
<TextView
android:id="#+id/textViewADV2"
android:text="Line 2"/>
My openActivity2() from Main Activity
public void openActivity2(View view)
{
Intent intentLoadNewActivity = new Intent(AdvancedResults.this,OpenSelectedAdvanced.class);
startActivity(intentLoadNewActivity);
}
I tried doing textViewADV1.getText().toString() but it only reads the text from the first item in the recycler view
My recycler adapter
public class AdvancedAdapter extends RecyclerView.Adapter<AdvancedAdapter.AdvancedViewHolder> {
private ArrayList<AdvancedItem> mAdvancedList;
public String mImage;
public static class AdvancedViewHolder extends RecyclerView.ViewHolder {
public ImageButton mImagebtn;
public TextView mTextView1;
public TextView mTextView2;
public AdvancedViewHolder(View itemView) {
super(itemView);
mImagebtn = itemView.findViewById(R.id.imageButtonADV);
mTextView1 = itemView.findViewById(R.id.textViewADV1);
mTextView2 = itemView.findViewById(R.id.textViewADV2);
}
}
public AdvancedAdapter(ArrayList<AdvancedItem> advancedList) {
mAdvancedList = advancedList;
}
#Override
public AdvancedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.advanced_item, parent, false);
AdvancedViewHolder evh = new AdvancedViewHolder(v);
return evh;
}
#Override
public void onBindViewHolder(AdvancedViewHolder holder, int position) {
AdvancedItem currentItem = mAdvancedList.get(position);
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
mImage = "www.testImageURL.com";
Picasso.get().load(mImage).into(holder.mImagebtn);
}
#Override
public int getItemCount() {
return mAdvancedList.size();
}
}
First of all pass a context to your adapter when you initialize it:
//in the main activity that you initialize the adapter in
AdvancedAdapter adapter = new AdvancedAdapter(your_list , MainActivity.this);
Now change the constructor of AdvancedAdapter to accept context
public class AdvancedAdapter extends RecyclerView.Adapter<AdvancedAdapter.AdvancedViewHolder> {
private ArrayList<AdvancedItem> mAdvancedList;
private Context context;
.............
//constructor
public AdvancedAdapter(ArrayList<AdvancedItem> advancedList,Context context) {
mAdvancedList = advancedList;
this.context = context;
}
Now in onBindViewHolder in the AdvancedAdapter
#Override
public void onBindViewHolder(AdvancedViewHolder holder, int position) {
AdvancedItem currentItem = mAdvancedList.get(position);
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
mImage = "www.testImageURL.com";
Picasso.get().load(mImage).into(holder.mImagebtn);
//on click image button
hodler.mImagebtn.setOnclickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Intent intent = new Intent(context , OpenSelectedAdvanced.class);
intent.putExtra("data" , currentItem.getText1());
context.startActivity(intent);
}
});
}
To get the data from OpenSelectedAdvanced activity:
//in oncreate method:
Intent intent = getIntent();
//this is your text view text that came from the clicked image button
String transferedText = intent.getStringExtra("data");
If you have declared your onBindViewHolder() in your adapter class so why are you starting activity from MainActivity. You should add setOnclicklistenr() in onBindViewHolder() to achieve the desired result that you want that is in following way :
public class AdvancedAdapter extends RecyclerView.Adapter<AdvancedAdapter.AdvancedViewHolder> {
private ArrayList<AdvancedItem> mAdvancedList;
//one more thing you have to create Context field so that you can
//you can start the activity from any context (From Any activity)
private Context mContext;
public String mImage;
public static class AdvancedViewHolder extends RecyclerView.ViewHolder {
public ImageButton mImagebtn;
public TextView mTextView1;
public TextView mTextView2;
public AdvancedViewHolder(View itemView) {
super(itemView);
mImagebtn = itemView.findViewById(R.id.imageButtonADV);
mTextView1 = itemView.findViewById(R.id.textViewADV1);
mTextView2 = itemView.findViewById(R.id.textViewADV2);
}
}
public AdvancedAdapter(ArrayList<AdvancedItem> advancedList, Context mContext) {
mAdvancedList = advancedList;
this.mContext = mContext;
}
#Override
public AdvancedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.advanced_item, parent, false);
AdvancedViewHolder evh = new AdvancedViewHolder(v);
return evh;
}
#Override
public void onBindViewHolder(AdvancedViewHolder holder, int position) {
AdvancedItem currentItem = mAdvancedList.get(position);
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
mImage = "www.testImageURL.com";
holder.mImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here You start your activity
Intent intent = new Intent(mContext, Activity2.class);
//you can putExtras here
mContext.startActivity(intent);
});
Picasso.get().load(mImage).into(holder.mImagebtn);
}
#Override
public int getItemCount() {
return mAdvancedList.size();
}
}
Now initialize this adapter in your activity where you implemented recyclerView And Send that arraylist into the parameters of that constructor of your adapter :) if did not understand yet you can try the tutorial on the link given below : https://www.youtube.com/watch?v=bIppSKk9afI

I am failing to make a succesful indent from adapter

I am setting this adapter for an RecyclerView, I am failing to make an indent from that adapter I don't know why can some body guide me what is going on here because I am not an expert in programming as well in this android.
Below is my Adapter class and Activity class.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDatasetname;
private Integer[] mexp;
private Context context;
public ToggleButton select;
Integer selectedcountint=0;
private Bitmap[] mpro;
private String[] mloc;
private String[] mobj;Context m;
private String[] mselected;
public ArrayList<String> nselected = new ArrayList<>();
public static class MyViewHolder extends RecyclerView.ViewHolder{
public CardView mCardView;
public TextView mTextView;
public TextView texp;
public Button pdf;
public ToggleButton select;
public ImageView pro;
public TextView loc;
public View layout;
Context context;
private RecyclerView.ViewHolder s;
public MyViewHolder(View v){
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
texp = (TextView) v.findViewById(R.id.setexp);
pdf = (Button) v.findViewById(R.id.moreinfo);
select = (ToggleButton) v.findViewById(R.id.select);
pro = (ImageView) v.findViewById(R.id.setpropic);
loc = (TextView) v.findViewById(R.id.setlocation);
}
void bind(String name, Integer exp, String location, final Bitmap image, final String obj ) {
mTextView.setText(name);
texp.setText(String.valueOf(exp)+" yrs");
pro.setImageBitmap(image);
loc.setText(location);
pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Dashboard.this, com.parse.starter.View.class);
intent.putExtra("no", "6382551203");
startActivity(intent);
}
});
}
}
public MyAdapter(Context s,String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){
mDatasetname = myDataset;
mexp=exp;
mpro=pro;
mloc=loc;
mobj=obj;
m=s;
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
context=parent.getContext();
View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
MyViewHolder vh = new MyViewHolder(vs);
return vh;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position){
holder.bind(mDatasetname[position],mexp[position],mloc[position],mpro[position],mobj[position]);
}
#Override
public int getItemCount() { return mDatasetname.length; }
}
Solution:
Instead of this:
Intent intent = new Intent(Dashboard.this, com.parse.starter.View.class);
intent.putExtra("no", "6382551203");
startActivity(intent);
Write like this:
Intent intent = new Intent(Dashboard.this, (your_destination_class_name).class);
intent.putExtra("no", "6382551203");
startActivity(intent);
For Example:
Intent intent = new Intent(Dashboard.this, RegisterActivity.class);
intent.putExtra("no", "6382551203");
startActivity(intent);
If you're destination class name is View.class then change it to something else.
If the above method doesn't work,
Replace:
View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
with:
View vs = LayoutInflater.from(m).inflate(R.layout.card_item, parent, false);
and try. Hope it works.
first make constructor, adapter wont work with out constructor, its a big miss please check
holder.pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,YourIndentClassName.class);
intent.putExtra("no", "6382551203");
context.startActivity(intent);
}
});
Edit your code with this:
pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, com.parse.starter.View.class/*should be a proper class name*/);
intent.putExtra("no", "6382551203");
context.startActivity(intent);
}
});
holder.pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,YourIndentClassName.class);
intent.putExtra("no", "6382551203");
context.startActivity(intent);
}
});
strong text
use this code in your onBindViewHolder method of RecyclerVieAdapter

How can I pass the data from view to another activity?

I have used CardView inside a RecyclerView in horizontal manner. Now what I want to do is, when user click on a card then the data which I have putted into the intent with the help of putExtra function should be passed to the next activity.
Here is the code
public class SectionListDataAdapter extends RecyclerView.Adapter<SectionListDataAdapter.SingleItemRowHolder> {
private List<Section> itemsList;
private Context mContext;
public SectionListDataAdapter(Context context, List<Section> itemsList) {
this.itemsList = itemsList;
this.mContext = context;
}
#Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, null);
SingleItemRowHolder mh = new SingleItemRowHolder(v);
return mh;
}
#Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {
Section singleItem = itemsList.get(i);
holder.tvTitle.setText(singleItem.getName());
holder.tvAuthor.setText(singleItem.getAuthorname());
holder.tvPremium.setText(singleItem.getPremium());
Glide.with(mContext)
.load(singleItem.getImage()
.into(holder.itemImage);
}
#Override
public int getItemCount() {
return (null != itemsList ? itemsList.size() : 0);
}
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
protected TextView tvTitle, tvAuthor, tvPremium;
protected ImageView itemImage;
public SingleItemRowHolder(final View view) {
super(view);
this.tvTitle = (TextView) view.findViewById(R.id.tvTitle1);
this.tvAuthor = (TextView) view.findViewById(R.id.tvTitle2);
this.tvPremium = (TextView) view.findViewById(R.id.txtSubText1);
this.itemImage = (ImageView) view.findViewById(R.id.itemImage);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), BooksDescription.class);
intent.putExtra("id", NavigationDrawer.ID.get());
mContext.startActivity(intent);
}
});
}
}
}
Here in the line intent.putExtra("id", NavigationDrawer.ID); NavigationDrawe.ID having 20 integer data. Now which function should I use to get the data of only those card which is clicked.

Getting RecyclerView.Adapter List from ViewHolder

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!

update data from intentservice on an activity

I have an activity with the following code and I am trying to update the data from IntentService by creating the instance of the class and calling the method to update but it's crashing with a null pointer exception at mAdapter. Is this the right way to pass data from IntentService to an activity?
COde:
public class MainActivity extends Activity {
RecyclerView rv;
SimpleStringRecyclerViewAdapter mAdapter;
public static final TypedValue mTypedValue = new TypedValue();
public static int mBackground;
public static List<String> mValues;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cheesecake_homepage_fragment_cheese_list);
rv = (RecyclerView) findViewById(R.id.recyclerview);
setupRecyclerView(rv);
send_msg("");
}
public void setupRecyclerView(RecyclerView recyclerView) {
mAdapter = new SimpleStringRecyclerViewAdapter(getBaseContext(),
getRandomSublist(Cheeses.sCheeseStrings, 3));
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(mAdapter);
}
private List<String> getRandomSublist(String[] array, int amount) {
ArrayList<String> list = new ArrayList<>(amount);
Random random = new Random();
while (list.size() < amount) {
list.add(array[random.nextInt(array.length)]);
}
return list;
}
public static class SimpleStringRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
public static class ViewHolder extends RecyclerView.ViewHolder {
public String mBoundString;
public final View mView;
public final ImageView mImageView;
public final TextView mTextView;
public ViewHolder(View view) {
super(view);
mView = view;
mImageView = (ImageView) view.findViewById(R.id.avatar);
mTextView = (TextView) view.findViewById(android.R.id.text1);
}
#Override
public String toString() {
return super.toString() + " '" + mTextView.getText();
}
}
public String getValueAt(int position) {
return mValues.get(position);
}
public SimpleStringRecyclerViewAdapter(Context context, List<String> items) {
context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
mBackground = mTypedValue.resourceId;
mValues = items;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cheesecake_homepage_list_item, parent, false);
view.setBackgroundResource(mBackground);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mBoundString = mValues.get(position);
holder.mTextView.setText(mValues.get(position));
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
/*Intent intent = new Intent(context, CheeseDetailActivity.class);
intent.putExtra(CheeseDetailActivity.EXTRA_NAME, holder.mBoundString);
context.startActivity(intent);*/
}
});
}
#Override
public int getItemCount() {
return mValues.size();
}
}
public void send_msg( String set_text){
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
List<String> uu = getRandomSublist(Cheeses.sCheeseStrings, 1);
mValues.addAll(uu);
mAdapter.notifyDataSetChanged();
}
});
}
}
In intent service:
MainActivity test = new MainActivity();
test.send_msg("");
You can not use MainActivity instance like that in your Intent service code.
There are multiple approach to update UI from service like, LocalBroadcastReceivers, Messengers, BoundedService etc.
You can find an example to update UI from service using LocalBroadcastManager here
update data from intentservice on an activity
By creating Object of class which is extending Activity or any other main application component is not right way for communication between components.
For with in process communication(as in your case want to send data from IntentService to Activity) use LocalBroadcastManager

Categories

Resources