hello am trying to open or launch detail activity using holder button to view detail description. how to do it in android studio
here is my code so far:
holder.cmdStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent specPage= new Intent(this, SpecActivity.class);
startActiviti(specPage);
}
});
this code show red and can not function. how to code this right to open the activity in android studio
Like that:-
holder.cmdStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent specPage= new Intent(v.getContext(), SpecActivity.class);
v.getContext().startActivity(specPage);
}
});
packageContext required by the intent might not be right, in this case "this". Also, you should not open an activity directly from the holder, instead use an router-listener approach.
Use Context passed inside onCreateViewHolder(). You can't use "this" as a Context inside onBindViewHolder()
#NonNull
#Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
mView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.recycler_view, viewGroup, false);
/* this one declared inside RecyclerAdapter*/
this.context = mView.getContext();
MyViewHolder vh = new MyViewHolder(mView);
return vh;
}
You can only use a context inside onBindViewHolder
holder.cmdStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent specPage= new Intent(context, SpecActivity.class);
context.startActiviti(specPage);
}
});
You are passing wrong object to Intent constructor, when you use 'this' inside onClickListener, you are passing onClickListener interface in place of context. You simply need to pass view context in place of 'this'.
Below code will work.
holder.cmdStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent specPage= new Intent(v.getContext(), SpecActivity.class);
v.getContext().startActivitiy(specPage);
}
});
Related
I want to (1) pass the position variable and (2) open a new activity when an item is clicked.
I found that I need to create Activity variable in order to use Intent, and I also found that I can use putExtra to pass the position variable.
Although the below code works for regular_activity.java, it does not work for fragment.java. It completely shuts down the app when I open this fragment.
#Override
public void onClick(View view) {
Intent int_detail = new Intent(activity, bottom_nav_search_details.class);
int_detail.putExtra("position", position);
activity.startActivity(int_detail); }
Just in case anyone finds useful, here is longer lines of codes. When I remove the above part (Intent part), everything works--Log successfully shows different positions when I click different items.
private Activity activity;
#Override
public void onBindViewHolder(#NonNull ExampleViewHolder holder, final int position) {
final ExampleItem currentItem = exampleList.get(position);
holder.imageView.setImageResource(currentItem.getImageResource());
holder.textView1.setText(currentItem.getText1());
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i(TAG, "Clicked " + position);
Intent int_detail = new Intent(activity, bottom_nav_search_details.class);
int_detail.putExtra("position", position);
activity.startActivity(int_detail);
}
});
}
I am trying to implement the onclick on the cardview in a recycler view. Currently I can only set the textview in a cardview to be clickable, like my code shows:
#Override
public void onBindViewHolder(final ShopViewHolder shopViewHolder, final int i) {
shopViewHolder.shopNameTV.setText(shops.get(i).name);
shopViewHolder.categoryTV.setText(shops.get(i).category);
shopViewHolder.phoneNumberTV.setText(shops.get(i).phoneNumber);
shopViewHolder.ratingsTV.setText(shops.get(i).ratings);
shopViewHolder.emailTV.setText(shops.get(i).email);
final Shop shop = shops.get(i);
shopViewHolder.shopNameTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("test", "onclickk here" + i);
// Intent intent = new Intent(RVAdapter.this,CustomerGetQueueActivity.class);
}
}) ;
}
Right now I can only set the shopNameTV to be clickable but I want the whole card to be clickable. When I tried to use shopViewHolder.setOnClickListener it shows me an error saying "cannot resolve method setOnClickListener(anonymous.android.view.View.OnClickListener).
How can I set the whole card to be clickable instead of just the textview inside the card ?
What you are doing is, applying the onClickListeners only to specific fields. That's why it is not applicable to whole view. You are almost there, all you need to do is.
shopViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("test", "onclickk here" + i);
//Intent intent = new Intent(RVAdapter.this,CustomerGetQueueActivity.class);
}
}) ;
public final View itemView
It is the parent view of your RecyclerView.ViewHolder.
EDIT
To resolve new problem, mentioned in comments:-
You will need to pass Application or component context as a first parameter to the Intent Constructor when you are creating an Intent for a specific component of your application.
Step 1 : Pass context to the constructor of Adapter
public class RVAdapter extend BaseAdapter{
Context mContext;
public RVAdapter(Context context, ... ) //Other parameters
{
mContext = context;
//Other code
}
Step 2 : Now use this context to start new Activity
Intent intent = new Intent(mContext, CustomerGetQueueActivity.class);
Save the view that is passed to your ShopViewHolder as a member variable name it for example cardView.
then use below code inside your onBindViewHolder:
shopViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}) ;
Is it wrong to do that or will it work just fine? I'm trying to make every relative layout clickable and when you click on it it takes you to a new page where the mGameTitle and mReleaseDate are passed to this new activity.
I'm doubting if it will work 'cause I'm getting an error with View.OnClickListener; must either be declared abstract or implement abstract method 'onClick(View)' in 'OnClickListener
public void onBindViewHolder(final ViewHolderUpcoming viewHolderUpcoming, int i) {
Game currentGame = listOfGames.get(i);
viewHolderUpcoming.mReleaseDate.setText(currentGame.getReleaseDate());
viewHolderUpcoming.mGameTitle.setText(currentGame.getTitle());
viewHolderUpcoming.mLayout.setOnClickListener(new View.OnClickListener() {
//Intent intent = new Intent(this, GamePage.class);
});
}
As the error states, you need to implement onClick(View).
viewHolderUpcoming.mLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
//TODO: Implementation
}
});
I have a syntax errors with my code , in the "getView" I want to make a listener for the button "update" to move to another activity :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater l = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View rowView = l.inflate(R.layout.temp, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
Button update = (Button) rowView.findViewById(R.id.button1);
// update.setOnClickListener(new View.OnClickListener() {
//
// #Override
// public void onClick(View v) {
//
// Intent redirect = new Intent(getApplicationContext(),Update.class);
// startActivity(redirect);
//
// }
// });
textView.setText(sa.get(position));
return rowView;
}
I've tried to fix these errors about "Intent" but I failed :(
The method startActivity(Intent) is undefined for the type new View.OnClickListener()
The method getApplicationContext() is undefined for the type new View.OnClickListener()
and even whene I moved these statements from "onClick" method the problem didn't change !!
I imported "Intent" library , how to solve that ????
If your adapter is in a different file you will need activity context.
You need to pass the activity context from your activity class to your adapter constructor.
http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)
startActivity is a method of your activity class. So you need activity context to start the activity.
Also instead of getApplicationContext use activity context.
When to call activity context OR application context?
Check the answer by commonsware.
update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent redirect = new Intent(context,Update.class);
context.startActivity(redirect);
}
});
Try
final Activity thisActivity = this;
update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent redirect = new Intent(thisActivity,Update.class);
thisActivity.startActivity(redirect);
}
});
for similar problem for me just this helped me :
#Override
public void onClick(View v) {
Intent intent = new Intent(G.context, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
G.context.startActivity(intent);
}
});
add context in G Class :
public class G extends Activity {
public static Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
}
}
You just need to call context.startActivity instead, can follow below steps
Three simple steps
1) Declare local Variable Context mCtx
2) intialize it with Activity context either by passing a parameter to constructor/method, or in the onCreate method if you are using above code in activity.
3) call mCtx.startActivity(intent);
or
you can call ActivityName.this.startActivity()
Edit:- As par dmon comment, You simply can use your local context instance
The root of your problem is that you cannot start an Activity from an application Context (only from an activity context you have the right to do that). The reason for that is that android bundles all activities of your application as a group (task) in order to allow multitasking between different apps. When you launch a new app, or press the home button for example, you are application (together with its activities backstack) is going to the background and leaves the scene to a new task (with a different activity backstack and so on and so forth). For more info on this mechanism you can take a look here (http://developer.android.com/guide/components/tasks-and-back-stack.html).
In order to gain a context that is ok to lauch a new activity from, you can always get the context of the parent of the view you are inflating, by calling viewgroup.getContext() - eventhough you will have to declare the viewgroup as final, which is ok since you shouldn't be touching the parent.
a draft of your getView:
#Override
public View getView(int position, View convertView, final ViewGroup parent) {
LayoutInflater l = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View rowView = l.inflate(R.layout.temp, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
Button update = (Button) rowView.findViewById(R.id.button1);
update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent redirect = new Intent(parent.getContext(),Update.class);
startActivity(redirect);
}
});
textView.setText(sa.get(position));
return rowView;
}
I have a navigation button that I use in each class and want to set its onClickListener in the class that contains all the common code. However I get a
"The method startActivity(Intent) is undefined for the type new View.OnClickListener(){}"
error in startActivity(i).
The relevant code is:
public static void initiateNavigationButton(Context context, View view, int layoutResource) {
final Context classContext = context;
LayoutInflater inflater = LayoutInflater.from(classContext);
View layout = inflater.inflate(layoutResource, (ViewGroup) view);
Button navigationButton = (Button) layout.findViewById(R.id.navigation_button);
navigationButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(classContext, HomeActivity.class);
startActivity(i);
}
});
}
You are inside an inner class that implements the View.OnClickListener.
That class does not have a startActivity method that you could call, so you need to wrap it in order to access the parent activity's method. Assuming that your class' name is 'MyActivity', change the line startActivity(i); to
MyActivity.this.startActivity(i);