Unable to start new activity using Intent - java

I am calling an activity and also transferring the values from adapter to the next activity the values are transferred but unable to star the activity and also there is no error just the next activity doesn't open
Holder:
public class ContactReadHolder extends RecyclerView.ViewHolder {
TextView contactName, contactPhone;
ImageButton contactCall;
public ContactReadHolder(View itemView) {
super(itemView);
context =itemView.getContext();
contactName = (TextView) itemView.findViewById(R.id.contact_list_name);
contactPhone = (TextView) itemView.findViewById(R.id.contact_list_phone);
contactCall = (ImageButton) itemView.findViewById(R.id.contact_list_call);
}
}
This s my adapter code and i have implented listner in the onBindViewHolder but still it soesn't work and also i have tried in Holder class also
Adapter:
public class ContactReadAdapter extends RecyclerView.Adapter<ContactReadAdapter.ContactReadHolder> {
private LayoutInflater layoutInflater;
private List<ContactListModel> contactListModelList;
private Context context;
public ContactReadAdapter(List<ContactListModel> contactListModelList, Context context) {
this.contactListModelList = contactListModelList;
this.context = context;
}
#NonNull
#Override
public ContactReadHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.abc_contact_layout, parent, false);
ContactReadHolder contactReadHolder = new ContactReadHolder(view);
return contactReadHolder;
}
#Override
public void onBindViewHolder(#NonNull final ContactReadHolder holder, int position) {
final TextView contactName, contactPhone;
contactName = holder.contactName;
contactPhone = holder.contactPhone;
contactName.setText(contactListModelList.get(position).getModel_contact_name());
contactPhone.setText(contactListModelList.get(position).getModel_contact_phone());
holder.contactCall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("duit","clicked ");
Intent intent=new Intent(context,OutgoingCallActivity.class);
String string_contactName=holder.contactName.getText().toString();
String string_contactPhone=holder.contactPhone.getText().toString();
Log.d("duit","clicked " + string_contactName +" "+ string_contactPhone);
intent.putExtra("user_name",string_contactName);
intent.putExtra("calleeNumber", string_contactPhone);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return contactListModelList.size();
}

Try this
Intent intent=new Intent(v.getContext(),OutgoingCallActivity.class);
// put extra methods
v.getContext().startActivity(intent);

Write your onclicklistener on onBindViewHolder function and it will work.

Intent intent=new Intent(contactName.getContext(),OutgoingCallActivity.class);
String string_contactName=holder.contactName.getText().toString();
String string_contactPhone=holder.contactPhone.getText().toString();
Log.d("duit","clicked " + string_contactName +" "+ string_contactPhone);
intent.putExtra("user_name",string_contactName);
intent.putExtra("calleeNumber", string_contactPhone);
contactName.getContext().startActivity(intent);
or
Intent intent=new Intent(holder.contactName.getContext(),OutgoingCallActivity.class);
String string_contactName=holder.contactName.getText().toString();
String string_contactPhone=holder.contactPhone.getText().toString();
Log.d("duit","clicked " + string_contactName +" "+ string_contactPhone);
intent.putExtra("user_name",string_contactName);
intent.putExtra("calleeNumber", string_contactPhone);
holder.contactName.getContext().startActivity(intent);

Related

Open 1 different activity for each cardview

I'm trying to figure how to go to another activity when I click on one item of a cardview (with recyclerview).
I searched on internet and this website and tried several options, but it isn't working (I tried only for position 0, the others will be done the same). Here, with the card 0 i try to go to "selection_niveau".
1st try : inside OnClick:
public class adapter_categorie_solo extends RecyclerView.Adapter<adapter_categorie_solo.ViewHolder> {
List<String> titles;
List<Integer> images;
LayoutInflater inflater;
public adapter_categorie_solo(Context ctx, List<String> titles, List<Integer> images){
this.titles = titles;
this.images = images;
this.inflater = LayoutInflater.from(ctx);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.cardview_solo_categorie,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.title.setText(titles.get(position));
holder.gridIcon.setImageResource(images.get(position));
}
#Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView gridIcon;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textView2);
gridIcon = itemView.findViewById(R.id.imageView2);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
if (getAdapterPosition()==0)
Intent intent = new Intent (this,selection_niveau.class);
}
});
}
}
}
I get this error :
error: variable declaration not allowed here
Intent intent = new Intent (this,selection_niveau.class);
2nd try : inside onBindViewHolder
public class adapter_categorie_solo extends RecyclerView.Adapter<adapter_categorie_solo.ViewHolder> {
List<String> titles;
List<Integer> images;
LayoutInflater inflater;
public adapter_categorie_solo(Context ctx, List<String> titles, List<Integer> images){
this.titles = titles;
this.images = images;
this.inflater = LayoutInflater.from(ctx);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.cardview_solo_categorie,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.title.setText(titles.get(position));
holder.gridIcon.setImageResource(images.get(position));
Intent intent;
if(position == 0)
intent = new Intent(this, selection_niveau.class);
holder.itemView.setOnClickListener(new View.OnClickListener(){
#Override public void onClick(View v){
Context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView gridIcon;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textView2);
gridIcon = itemView.findViewById(R.id.imageView2);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
And I get this error :
error: no suitable constructor found for Intent(adapter_categorie_solo,Class<selection_niveau>)
intent = new Intent(this, selection_niveau.class);
^
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; adapter_categorie_solo cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; adapter_categorie_solo cannot be converted to Context)
I tried to change "this" to only "context", but it doesn't work either.
These are some of the solutions I found on internet and here, but none is working and I can't really understand why as being a beginner.
Inside the onClick() callback this keyword will points to the anonymous inner class of the View.OnClickListener; so you need to change to a context by using v.getContext() instead.
Apply this by replacing itemView.setOnClickListener with:
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
if (getAdapterPosition()==0) {
Intent intent = new Intent (v.getContext(), selection_niveau.class);
}
}
});
Intent constructor requires a context as the first argument, but this in the ViewHolder refers to the instance of ViewHolder, So you need to get context from the view or use the context reference you got in the constructor of the adapter_categorie_solo.
public class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView gridIcon;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textView2);
gridIcon = itemView.findViewById(R.id.imageView2);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked -> " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
if (getAdapterPosition()==0)
{
Intent intent = new Intent (ctx,selection_niveau.class);
ctx.startActivity(intent);
}
}
});
}
}
Using switch instead of if will be an optimal approach if you want to start different activity based on click position.
use context in Intent instead of this
if(position == 0)
intent = new Intent(context, selection_niveau.class);

Android. java.lang.NoClassDefFoundError

I got the following error
java.lang.NoClassDefFoundError: Failed resolution of: Lcom / scandevices / DevicesAdapter;
about a custom adapter that I created, extending the class with RecyclerView.Adapter, I don't understand why in the main thread it gives me this error, in all the applications that I develop.
Code of custom adapter:
class DevicesAdapter extends RecyclerView.Adapter<DevicesAdapter.Device> {
Context context;
Vector<String> names;
DevicesAdapter(Context _context, Vector<String> _names) {
context = _context;
names = _names;
}
#Override
public Device onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.device_row, parent, false);
return new Device(view);
}
#Override
public void onBindViewHolder(Device holder, int position) {
holder.name.setText(names.elementAt(position));
}
#Override
public int getItemCount() {
return names.size();
}
class Device extends RecyclerView.ViewHolder {
TextView name;
Button open;
public Device(View itemview) {
super(itemview);
name = (TextView) itemview.findViewById(R.id.ip_device);
open = (Button) itemview.findViewById(R.id.open_device);
open.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
HomeFragment.finishscan = true;
Intent intent = new Intent(context, ControllerActivity.class);
intent.putExtra("name", name.getText().toString());
context.startActivity(intent);
}
});
}
}
}
Where i got the error:
devicesadapter = new DevicesAdapter(context, names); /// ERROR
recyclerdevices = v.findViewById(R.id.recycler_devices);
recyclerdevices.setAdapter(devicesadapter);
recyclerdevices.setLayoutManager(new LinearLayoutManager(context));
I got also this error:
I/System.out: [socket] e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaUtils

value passing from intent getExtras returns null value from first Activity to Second Activity

I'm trying to pass the username from BlogRecycle adapter to Next activity. But In second Activity it returns null value.
Here is BlogRecycleAdapter Class code
public class BlogRecyclerAdapter extends RecyclerView.Adapter<BlogRecyclerAdapter.ViewHolder> {
public List<BlogPost> blog_list;
public Context context;
private FirebaseFirestore firebaseFirestore;
private FirebaseAuth firebaseAuth;
private PopupWindow popWindow;
public BlogRecyclerAdapter(List<BlogPost> blog_list){
this.blog_list = blog_list;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item, parent, false);
context = parent.getContext();
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseAuth = FirebaseAuth.getInstance();
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.setIsRecyclable(false);
final String blogPostId = blog_list.get(position).BlogPostId;
final String currentUserId = firebaseAuth.getCurrentUser().getUid();
String desc_data = blog_list.get(position).getDesc();
holder.setDescText(desc_data);
String image_url = blog_list.get(position).getImage_url();
String thumbUri = blog_list.get(position).getImage_thumb();
holder.setBlogImage(image_url, thumbUri);
String user_id = blog_list.get(position).getUser_id();
//User Data will be retrieved here...
firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
String userName = task.getResult().getString("name");
String userImage = task.getResult().getString("image");
holder.setUserData(userName, userImage);
} else {
//Firebase Exception
}
}
});
#Override
public int getItemCount() {
return blog_list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private View mView;
private TextView blogUserName;
private CircleImageView blogUserImage;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
blogLikeBtn = mView.findViewById(R.id.blog_like_btn);
blogCommentBtn = mView.findViewById(R.id.blog_comment_icon);
}
public void setUserData(final String name, final String image) {
blogUserImage = mView.findViewById(R.id.blog_user_image);
blogUserName = mView.findViewById(R.id.blog_user_name);
blogUserName.setText(name);
RequestOptions placeholderOption = new RequestOptions();
placeholderOption.placeholder(R.drawable.profile_placeholder);
if (!((Activity) context).isFinishing()) {
Glide.with(context).applyDefaultRequestOptions(placeholderOption).load(image).into(blogUserImage);
}
mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("user_name", name);
intent.putExtra("user_image", image);
context.startActivity(intent);
}
});
}
}
and In my Second Activity Class is
final String name = getIntent().getStringExtra("user_name");
Toast.makeText(getBaseContext(), "Logged in as"+name, Toast.LENGTH_SHORT).show();
I am trying to use name from BlogRecycle Adapter class to Second Activity Class but when I pass the the value from BlogRecycle Adapter to Second Activity and compile and run app it returns.
Logged in as !null
Any help is appreciated. Thanks in advance.
I think the code line:
Intent intent = new Intent(context, MainActivity.class);
should be :
Intent intent = new Intent(context, SecondActivity.class);

RecyclerView and onclicklistner operation

I'm fetching RSS feed into my APP and trying to open an intent(Web browser) when a one of the news of recyclerView is clicked, but it dosn't work. and that's my code:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<FeedItem> feedItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
//Call Read rss asyntask to fetch rss
ReadRss readRss = new ReadRss(this, recyclerView);
readRss.execute();
recyclerView.setOnClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
**FeedItem current=feedItems.get(position);
Uri uri = Uri.parse(current.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);**
}
});
}
}
I did even try to put the same code in my holder class but in there the startActivity() method don't work:
the above code is kinda the same but in another class:
public class FeedsAdapter extends RecyclerView.Adapter<FeedsAdapter.MyViewHolder> implements View.OnClickListener {
ArrayList<FeedItem> feedItems;
Context context;
public FeedsAdapter(Context context, ArrayList<FeedItem>feedItems){
this.feedItems=feedItems;
this.context=context;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.custum_row_news_item,parent,false);
view.setClickable(true);
view.setOnClickListener(this);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
YoYo.with(Techniques.FadeIn).playOn(holder.cardView);
FeedItem current=feedItems.get(position);
holder.Title.setText(current.getTitle());
holder.Description.setText(current.getDescription());
holder.Date.setText(current.getPubDate());
Picasso.get().load(current.getThumbnailUrl()).into(holder.Thumbnail);
holder.cardView.setTag(position);
Toast.makeText(context,current.getLink() ,Toast.LENGTH_SHORT).show();
}
#Override
public int getItemCount() {
return feedItems.size();
}
#Override
public void onClick(View view) {
**int position = (int) view.getTag();
FeedItem current=feedItems.get(position);
Toast.makeText(context,"The Item Clicked is: "+current.getLink() ,Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse(current.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);**
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView Title,Description,Date;
ImageView Thumbnail;
CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
Title= (TextView) itemView.findViewById(R.id.title_text);
Description= (TextView) itemView.findViewById(R.id.description_text);
Date= (TextView) itemView.findViewById(R.id.date_text);
Thumbnail= (ImageView) itemView.findViewById(R.id.thumb_img);
cardView= (CardView) itemView.findViewById(R.id.cardview);
}
}
}
this code:
FeedItem current=feedItems.get(position);
Uri uri = Uri.parse(current.getLink());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
is the same code that I tried in two classes in the first the error is because of RecyclerView, And in the second one it gives error because startActivity don't work in that class.
So how do I activate the clicakble news that when clicked goes to the browser?
To start activity from within the adapter class use
context.startActivity(intent);
( if thats your question.)
My suggestion
Set the click listener in onBindViewHolder(..) of your adapter on holder.itemView.setOnClick....

How to pass data from Recyclerview onClickLister to other Activity

I want to pass data from onclick recycler view main activity to other activity(exoplayer).
I created onimageclicklistener class and then I declared my onimageclicklistener in the adapter class. Then I implement that listener in main activity and implemented on image click method, now I am blank and do not know what do.
I will appreciate if someone helps.
interface class:
public interface OnImageClickListener
{
void onImageClick(String imageData);
}
adapter:
public class SongAdapter extends RecyclerView.Adapter {
private Context context;
private List<Datum> datums;
//imagelistener define
private OnImageClickListener onImageClickListener;
//3rd argument is listener
public SongAdapter(Context context, List<Datum> datums, OnImageClickListener onImageClickListener) {
this.context = context;
this.datums = datums;
this.onImageClickListener = onImageClickListener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Datum datum = datums.get(position);
Glide.with(context).load(datums.get(position).getThumbnail()).into(holder.thumbnail);
holder.title.setText(datum.getTitle());
holder.artist.setText(datum.getArtist());
// holder.duration.setText(datum.getDuration());
holder.duration.setText(Integer.toString(datum.getDuration()));
//imageview interface pass
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onImageClickListener.onImageClick(datum.getUrl());
}
});
}
#Override
public int getItemCount() {
return datums.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView thumbnail;
TextView title;
TextView artist;
TextView duration;
public ViewHolder(#NonNull View itemView) {
super(itemView);
thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
title = (TextView) itemView.findViewById(R.id.title);
artist = (TextView) itemView.findViewById(R.id.artist);
duration = (TextView) itemView.findViewById(R.id.duration);
}
}
}
Mainactivity:
//imageclick listener
#Override
public void onImageClick(String imageData) {
Intent i = new Intent(MainActivity.this,exoplayer.class);
startActivity(i);
}
I want to send data to exoplayer class.
Using Intent Extras
#Override
public void onImageClick(String imageData) {
Intent i = new Intent(MainActivity.this,exoplayer.class);
intent.putExtra("YOUR_KEY_STRING", imageDate);
startActivity(i);
}
And then from exoplayer Activity, you retrieve it in the onCreate() method as follows :
String imageData = getIntent().getStringExtra("YOUR_KEY_STRING");
Using bundle
#Override
public void onImageClick(String imageData) {
Intent i = new Intent(MainActivity.this,exoplayer.class);
Bundle bundle = new Bundle();
bundle.putString("imageDataKey", imageData);
intent.putExtra("YOUR_BUNDLE_KEY", bundle);
startActivity(i);
}
And then from your exoactivity you retrieve it as follows :
if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getStringExtra("YOUR_BUNDLE_KEY");
String imageData = bundle.getString("imageDataKey");
}
Using intent we can send data
//imageclick listener
#Override
public void onImageClick(String imageData) {
Intent i = new Intent(MainActivity.this,exoplayer.class);
i.putExtra("data",imageData);
startActivity(i);
}

Categories

Resources