问题
Actually I'm trying to pass intent from adapter class on click of item . Although it may sound a normal or foolish question but I'm stuck in it from last 3 4 days . At first I was thinking that the itemView
is not receiving the click but then I tried to put a toast inside the onClick()
method but the strange part is it's toasting the message without any problem or error but it's not passing intent . I don't know why this strange behavior is happening . I've tried to search a lot of questions in SO but none of them were helpful.
Code:
//opening img on clik
holder.itemView.setClickable(true);
holder.itemView.setFocusable(true);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ctx,"testing",Toast.LENGTH_SHORT).show();
Bundle b = new Bundle();
b.putString("what", "show_post");
b.putString("timelineData", gson.toJson(totalList));
b.putInt("position",holder.getAdapterPosition());
Intent i = new Intent(ctx, ProfileHolder.class);
i.putExtras(b);
i.putExtra("Open", "starred");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);
}
});
回答1:
- remove
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
you don't need that - is
ProfileHolder
an Activity? It doesn't seem to be one. - if
ProfileHolder
is really an Activity, check that it is registered in the AndroidManifest.xml file.
回答2:
Make interface between adapter and activity its best way to pass intent usefull link: "https://github.com/Androidsignal/Android-Retrofit" see in ada
回答3:
I think you are not passing context properly.
Do Below steps: 1.Declare Context globally in the adapter.
Context context;
2.In onCreateViewHolder write below code.
context = viewGroup.getContext();
3.Now on click
context.startActivity(new Intent(context, Activity.class);
来源:https://stackoverflow.com/questions/49990416/why-intent-is-not-passed-from-adapter-class