I have RecyclerViewAdapter :
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyViewHolder> {
private List<Cards> items;
private int itemLayout;
Context context;
public RecycleViewAdapter(List<Cards> items, int itemLayout) {
this.items = items;
this.itemLayout= itemLayout;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(itemLayout,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Cards item=items.get(position);
holder.title.setText(item.getName());
String cardvalue = item.getCountry();
String cardCode = item.getCode();
String cardCountry = item.getCountry();
}
#Override
public int getItemCount() {
return items.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView title;
public MyViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.listText);
title.setOnClickListener(this);
}
#Override
public void onClick(View v) {
}
}
}
and I need to start new Intent (OnCardsSelected.class) , but I cant add this line to my method onClick:
Intent intent = new Intent (this, OnCardSelected.class);
I don't understand whats I do wrong, I just need to start Intent and put some information to this intent, I read some manuals but didn't understand theirs explanation so I hope u will help me .Thanks
Probably trying to start Activity from onClick method then use v.getContext() instead of this(which refer to onClick method context) as first parameter to Intent constructor :
Intent intent = new Intent (v.getContext(), OnCardSelected.class);
Add this line of code in your onBindViewHolder method
Intent intent = new Intent(v.getContext(), YourClassName.class);
v.getContext().startActivity(intent);
And you're good to go.
public RecycleViewAdapter(Context context,List<Cards> items, int itemLayout) {
this.mContext = context;
this.items = items;
this.itemLayout= itemLayout;
}
...
Intent intent = new Intent(mContext, YOUR_ACTIVITY.class);
mContext.startActivity(intent)
Intent i = new Intent(holder.itemView.getContext(), NewOnCLickAccount.class);
holder.itemView.getContext().startActivity(i);
works fine for me.
This can save someone, especialy one with different activities to open for each item click. I was in same situation.
Create a model class (or restructure your old one) to add Intent . Here is my example.
Class SampleModel{
String sampleString;
Intent sampleIntent;
public SampleModel(String sampleString, Intent sampleIntent){
this.sampleString = sampleString;
this.sampleIntent = sampleIntent;
}
//add getters
}
In your recyclerview adapter add Context (let's call it sampleContext) and ArrayList (lets call it list)that will carry your model.
In your onBindViewHolder
SampleModel model = list.get(position);
//inside holder.viewclick setOnClickListner, here we open intent
sampleContext.starActivity(model.getIntent);
The rest is just like normal recyclerview process. i.e
SampleModel model = new SampleModel ("sample text", new Intent(this, "AnotherActivity.class));
Here is an example of me using an OnClickListener inside the adapter's BindViewHolder
#Override
public void onBindViewHolder(#NonNull ItemHolder holder, int position) {
Item playerItem = (Item) itemList.get(position);
itemName = playerItem.getName();
itemPosition = holder.getAdapterPosition();
itemHolder = holder;
//Sets Text
holder.name.setText(itemName);
//holder.playerName.setTag(R.string.listSize, playerList.size());
holder.name.setTag(position);
//holder.image.setImageAlpha(playerItem.getImage());
holder.image.setImageResource(playerItem.getImage());
holder.image.setTag(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//goes to new activity passing the item name
Intent intent = new Intent(itemHolder.itemView.getContext(), MapActivity.class);
Bundle b = new Bundle();
//get text for current item
String textGet = itemName;
//put text into a bundle and add to intent
intent.putExtra("text", textGet);
//get position to carry integer
intent.putExtra("position", itemPosition);
intent.putExtras(b);
//begin activity
itemHolder.itemView.getContext().startActivity(intent);
}
});
}
You already Passing the Context into the adapter. Therefore inside the onBindViewHolder() you can create the intent.
Intent intent = new Intent(context,OnCardsSelected.class);
context.startActivity(intent);
Related
I created six of CardView and linked them to RecyclerView , how when press on cardview postion[2]
I want to make every card view and guest a move to another activity
this my code.
public class MyMovieAdapter extends RecyclerView.Adapter<MyMovieAdapter.ViewHolder> {
MyMovieData[] myMovieData;
Context context;
public MyMovieAdapter(MyMovieData[] myMovieData,MainActivity activity) {
this.myMovieData = myMovieData;
this.context = activity;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.movie_item_list,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final MyMovieData myMovieDataList = myMovieData[position];
holder.textViewName.setText(myMovieDataList.getMovieName());
holder.textViewDate.setText(myMovieDataList.getMovieDate());
holder.movieImage.setImageResource(myMovieDataList.getMovieImage());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, myMovieDataList.getMovieName(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(v.getContext(), MainActivity2.class);//////////////// //this line //////////////////////// I want to position id
v.getContext().startActivity(intent);
}
});
}
you already have int position, just make it final and then you may use it inside onClick
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
...
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, myMovieDataList.getMovieName(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(v.getContext(), MainActivity2.class);
intent.putExtra("position", position);
// would be better to pass item id or name, much better approach
intent.putExtra("itemId", myMovieDataList.getMovieId());
v.getContext().startActivity(intent);
}
});
}
Answer by #snachman is perfectly right. But, it is not preferred by google. Use the method given below.
Create a new interface with name RecyclerViewItemClickListener.java
Add method void onClick(); to it;
Add that to the constructor of the adapter.
After adding the constructor, add it to your activity by implementing the interface
Add it in the activity by new RecyclerViewItemClickListener
Now in the activity, add the code to start the activity.
Hope it helps 😀
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
I'm building a to do app and I'm trying to add the edit function. When an item is clicked, it takes the user to EditItemActivity.java where the user can edit that particular to do. For example, it could be edited from "Do Homework" to "Go to the gym".
So far I've been able to get the item that was clicked and have its text display in the EditText field that will be used for the editing. However, I couldn't figure out how to save the edit when the button is clicked and go back to the main screen.
So in my adapter, I'm calling the intent with extra:
public class ToDoAdapter extends RecyclerView.Adapter<ToDoAdapter.ViewHolder> {
private List<ToDoData> toDoList;
private Context context;
public ToDoAdapter(List<ToDoData> todoList, Context context) {
this.toDoList = todoList;
this.context = context;
}
public ToDoAdapter(Context context){}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView todoView;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
todoView = itemView.findViewById(R.id.to_do_display);
}
#Override
public void onClick(View view) {
Toast.makeText(context, "position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, EditItemActivity.class);
intent.putExtra("data", todoView.getText());
intent.putExtra("position", getAdapterPosition());
context.startActivity(intent);
}
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_do_list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ToDoData todoPosition = toDoList.get(position);
holder.todoView.setText(todoPosition.getToDo());
}
#Override
public int getItemCount() {
return (toDoList == null) ? 0 : toDoList.size();
}
}
And in my EditItemActivity.java, I'm retrieving the text and placing it in my EditText field:
public class EditItemActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_item);
final ToDoData toDoData = new ToDoData(this);
final ToDoAdapter toDoAdapter = new ToDoAdapter(this);
final EditText editToDo = (EditText)findViewById(R.id.edit_todo_item);
Button button = (Button)findViewById(R.id.save_changes);
final String text = getIntent().getExtras().getString("data");
editToDo.setText(text);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(editToDo.getText().toString() != text){
toDoData.setToDo(editToDo.getText().toString());
toDoAdapter.notifyDataSetChanged();
finish();
}
}
});
}
}
But I don't know how to go further than this unfortunately.
what you will do is that :
1- send view position to edit activity
#Override
public void onClick(View view) {
Toast.makeText(context, "position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, EditItemActivity.class);
intent.putExtra("data", todoView.getText());
intent.putExtra("position", getAdapterPosition());
context.startActivity(intent);
}
2- in edit activity
final String position= getIntent().getExtras().getString("position");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new intent(this, [PREVIOUS_ACTIVITY]);
intent.putExtra("new data", todoView.getText().toString());
intent.putExtra("position", position);
startActivity(intent);
}
});
4 - in activity which contains the adapter create hashmap hold the upcoming extras (position - text) as ( key - value )
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(position, newText);
5- then pass this hash map to your adapter class when you create its instance. and in onBindViewHolder() check the upcoming view position is contained in hashmap if yes set your new data
if(hashMap.containsKey(position))
view.todoView.setText(hashMap.get(position));
I started in the android programming and and I encountered a problem when i tried to send data from my Adapter to an another activity
.
I want to show the content of "description" in my other activity when I click on an item.
MyAdapter class :
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private final List<FakeNews> list = FakeNewsList.all;
#Override
public int getItemCount() {
return list.size();
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.list_cell, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
FakeNews pair = list.get(position);
holder.display(pair);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView name;
private final TextView description;
private FakeNews currentPair;
public MyViewHolder(final View itemView) {
super(itemView);
name = ((TextView) itemView.findViewById(R.id.name));
description = ((TextView) itemView.findViewById(R.id.description));
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MyViewHolder.this ,Affich.class);
intent.putExtra("description", String.valueOf((description)));
startActivity(intent);
}
});
}
public void display(FakeNews pair) {
currentPair = pair;
name.setText(pair.title);
}
}
}
Can someone help me?
I assume that you do not really want to pass the TextView to your next activity, but the content which is shown in it. Also, you need to pass a Context to the Intent and startActivity() is a method of an activity, so you need to call it on the context:
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext() ,Affich.class);
intent.putExtra("description", description.getText().toString());
view.getContext().startActivity(intent);
}
});
Pass the Context or Activity instance into your adapter class.
then while starting new Activity and suppose instance variable is mContext use like this-
Intent intent = new Intent(mContext ,Affich.class);
intent.putExtra("description", String.valueOf((description)));
mContext.startActivity(intent);
To pass value from TextView to your new activity, do this:
intent.putExtra("description", description.getText().toString());
on your new activity, get the description value using:
String descriptionValue = getIntent().getStringExtra("description");
Within my Recycler Adapter, I have the following:
public void onBindViewHolder(ViewHolder holder, int position) {
User userItem = mDataset.get(position);
holder.itemView.setTag(userItem);
...
}
Within my layout, I have a clickable element:
...
<FrameLayout
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#color/white"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:onClick="showActivity">
...
On click, it should show the next Activity. I need to pass the tag that I attached to it so that I can use the data in the next Activity.
Here is showActivity within my activity:
public void showActivity(View view) {
Intent intent = new Intent(this, SecondActivity.class);
// pass the model to the next activity
startActivity(intent);
}
How can I do this?
When you need to pass data from one activity to another, you use Bundle. Unfortunately you can't pass any object you want, you need convert it to Bundle.
Also, where is this onBindViewHolder() method? If it is in Activity, just save the data in a class variable and use it on showActivity(). If it is in adapter, you can have a public method to achieve an item's tag and use that method in the activity.
Your question is not clear but i think you want to pass selected item to next activity but instead passing its model just pass its data by bundle in click listener.
Try this:
public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder> {
private LayoutInflater inflater;
private Context context;
List<Information>data= Collections.emptyList();
public AdapterClass(Context context,List<Information>data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= inflater.inflate(R.layout.custom_row,parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Information current=data.get(position);
holder.title.setText(current.title);
holder.icon.setImageResource(current.iconId);
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private final Context context;
TextView title;
ImageView icon;
public MyViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
title=(TextView)itemView.findViewById(R.id.listText);
icon=(ImageView)itemView.findViewById(R.id.listIcon);
//Set click listener on list item
itemView.setClickable(true);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
//Get data of clicked item
Information item = data.get(getPosition());
Intent intent = new Intent(context, NextActivity.class);
//Create bundle which include title and icon and pass it to nextactivity
Bundle bundle = new Bundle();
bundle.putString("KEY_LIST_ITEM_TITLE", item.title);
bundle.putInt("KEY_LIST_ITEM_ICON", item.iconId);
//set bundle to intent
intent.putExtras(bundle);
context.startActivity(intent);
}
};
}
Now in NextActivity simply get title and icon from bundle
onCreate() {
.....
String title = getIntent().getExtras().getString("KEY_LIST_ITEM_TITLE");
int icon = getIntent().getExtras().getString("KEY_LIST_ITEM_ICON");
}
Note: if you want to pass object then use bundle.putParceable("ANY_KEY",object) and object class need to implement Parceable interface.
Your model should implement Parcelable,like that:
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
Then use that (intent.putExtra("mymodel",model);)pass your model to the second Activity.
Whenever you want to pass the data from one activity to other activity , you can pass it using bundle and intent. As you are using intent to start second activity,you can set data and in second activity you can get the same data using set like this :-
Intent mIntent = new Intent(this, SecondActivity.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);
or
Intent mIntent = new Intent(this, SecondActivity.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
or
Intent mIntent = new Intent(this, SecondActivity.class);
mIntent.putExtra(key, value);
and get those values like this:-
String value = getIntent().getExtras().getString(key) ;