Intent created but the startActivity method didn't identify - android

I tried to set an Onclick event for my cardView. As you can see, I created that event inside the OnBindViewHolder but the problem is that the startActivity method didn't identify.What do you think i should do? Thank you in advance and sorry for my English :)
public class AdapterVRList extends RecyclerView.Adapter<AdapterVRList.CourseViewHolder> {
private Context context;
private List<DObjectVrList> DObjectVrListList;
public AdapterVRList(Context context, List<DObjectVrList> DObjectVrListList){
this.context = context;
this.DObjectVrListList = DObjectVrListList;
}
public AdapterVRList(Context context){
}
#Override
public CourseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.card_vr,parent,false);
return new CourseViewHolder(view);
}
#Override
public void onBindViewHolder(CourseViewHolder holder, int position) {
DObjectVrList DObjectVrList = DObjectVrListList.get(position);
holder.CourseText.setText(DObjectVrList.getTitleCourse());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CourseInnerPage course = new CourseInnerPage(getItemCount());
Intent intent = new Intent(context,CourseInnerPage.class);
startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return 8;
}
public class CourseViewHolder extends RecyclerView.ViewHolder{
private TextView CourseText;
public CourseViewHolder(View itemView) {
super(itemView);
CourseText = (TextView) itemView.findViewById(R.id.course_text);
}
}
}
The error is: Cannot resolve the startActivity method

Because your adapter or its superclasses do not have the method startActivity().
Use context.startActivity();

use context.startActivity(intent)

Related

How to finish activity in OnBindViewHolder()

In my recycler view item I have a button so when user clicks on button user is going from one activity to another.
BUt when user is coming back from another activity and presses android back button then it backtracks activity on which recycler view is present.
Here is my code:
public class HomeBookAdapter extends RecyclerView.Adapter<HomeBookAdapter.ViewHolder> {
ArrayList<LoadHomeBooks> list;
Context context;
public HomeBookAdapter(ArrayList<LoadHomeBooks> list,Context context){
this.list = list;
this.context = context;
}
#NonNull
#Override
public HomeBookAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_book_layout,viewGroup,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull final HomeBookAdapter.ViewHolder viewHolder, int i) {
LoadHomeBooks model = list.get(i);
final String bookId = model.getbUid();
viewHolder.homeMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context,BookDetail.class);
i.putExtra("bId",bookId);
context.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
Button homeMore;
public ViewHolder(#NonNull View itemView) {
super(itemView);
homeMore = itemView.findViewById(R.id.homeMore);
}
}
}
Someone please let me know what I am doing wrong.
THANKS
Add this
Intent i = new Intent(context,BookDetail.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);

How to start new activity from recyclerview?

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);

Android RecyclerView how to open new activity from clicking the items

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();
}
});

error onclicklistener on recyclerview

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);
}
}

RecyclerView OnItemclick not working properly

I have a recycler view and I am implementing OnClicklistener inside it. Basically , I have an adapter class called actressadapter and a viewholder class MyViewHolder.I am implementing OnClickListener inside viewholder class to initiate another activity via intent method. My basic data is inside a class called actress which has three variables name,country(both String) and an Id(UUID). I am providing this data that is of actresses to my adapter.Next activity is just displaying the name of actress like abc ,def etc that it retrieves . The fault is that it shows name of only one actress after clicking. For instance if it shows abc ,then for each click it will show abc.Don't know why is this happening because as per code I am passing actressname as an extra .
public class actressadapter extends RecyclerView.Adapter <actressadapter.MyViewHolder> {
private List<Actress>al;
private Actress actress,mActress;
public static final String str="shivam.panwar.actressdetails.actressadapter.str";
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView naam, desh;
public MyViewHolder(View view) {
super(view);
view.setOnClickListener(this);
naam = (TextView) view.findViewById(R.id.name);
desh= (TextView) view.findViewById(R.id.country);
}
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(v.getContext(),Actressview.class);
intent.putExtra(str,actress.getName());
v.getContext().startActivity(intent);
}
public void bindactress(Actress mActress) {
naam.setText(mActress.getName());
desh.setText(mActress.getCountry());
}
}
public actressadapter(List<Actress> al) {
this.al = al;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.actress_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
actress =al.get(position);
holder.bindactress(actress);
}
#Override
public int getItemCount() {
return al.size();
}
}
If required any further assist about code please comment.
try this way hope it works...
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
actress =al.get(position);
holder.bindactress(actress);
holder.naam.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,holder.naam.getText().toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context,Actressview.class);
intent.putExtra(str,holder.naam.getText().toString());
context.startActivity(intent);
}
});
}
The best way to use the method getLayoutPosition() of adapter class to get the selected item(position).
You have to update your code for better result. Replace your existing code with that.
public class actressadapter extends RecyclerView.Adapter <actressadapter.MyViewHolder> {
private List<Actress>al;
private Actress actress,mActress;
private int itemPosition; // change
public static final String str="shivam.panwar.actressdetails.actressadapter.str";
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView naam, desh;
public MyViewHolder(View view) {
super(view);
view.setOnClickListener(this);
naam = (TextView) view.findViewById(R.id.name);
desh= (TextView) view.findViewById(R.id.country);
}
#Override
public void onClick(View v) {
itemPosition = getLayoutPosition(); // change
Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(v.getContext(),Actressview.class);
intent.putExtra(str,al.get(itemPosition).getName()); // change
v.getContext().startActivity(intent);
}
public void bindactress(Actress mActress) {
naam.setText(mActress.getName());
desh.setText(mActress.getCountry());
}
}
public actressadapter(List<Actress> al) {
this.al = al;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.actress_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
actress =al.get(position);
holder.bindactress(actress);
}
#Override
public int getItemCount() {
return al.size();
}
}
Hope it will work..
your issue is that you made actress a variable of the Adapte, and it should be of the Holder.
To fix it:
Delete the line:
private Actress actress,mActress;
Change the onBindViewHolder method to:
holder.bindactress(al.get(position))
And make replace your holder class with that:
// holder HAVE TO be static. It's WRONG not doing it static
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView naam, desh;
Actress actress;
public MyViewHolder(View view) {
super(view);
view.setOnClickListener(this);
naam = (TextView) view.findViewById(R.id.name);
desh= (TextView) view.findViewById(R.id.country);
}
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(v.getContext(),Actressview.class);
intent.putExtra(str,actress.getName());
v.getContext().startActivity(intent);
}
public void bindactress(Actress mActress) {
this.actress = mActress;
naam.setText(mActress.getName());
desh.setText(mActress.getCountry());
}
}

Categories

Resources