This question already has answers here:
How to open a different activity on recyclerView item onclick
(10 answers)
Closed 7 years ago.
I need to start an activity based on the item a user clicks on a RecyclerView. The code below has the position as a reference. Does anyone knows how to get this done? I need something like Intent intent = new Intent (MainActivity.this, Target.class). The target class changes depending on the item clicked of course.
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
Intent intent = new Intent(MainActivity.this, ???);
startActivity(intent);
}
})
);
Well, how about just putting the proper OnClickListener on each item's View in the RecyclerView? Each OnClickListener could store the target Class that you need for handling the navigation. You can handle this in the binding phase of the RecyclerView's adapter, there's not magic to it.
Select the target class via position:
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
switch(position){
case 0:
startActivity(new Intent(MainActivity.this, A.class));
break;
case 1:
startActivity(new Intent(MainActivity.this, B.class));
break;
default:
break;
}
}
})
);
Of course, you have to define the mapping from position to target class.
You have collection of objects (probably ArrayList),try to add Object which has field of Class type, and then get it like this:
Intent intent = new Intent(MainActivity.this, objects.get(position).getClassField());
startActivity(intent);
You just need to put a onclicklistener to your viewholder(contain views).
private MainActivity mAct;
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAct.animateActivity(anything);
}
});
public void animateActivity(anything any) {
Intent i = new Intent(this, AssetDescription.class);
//Some anitmation if you want
startActivity(i);
}
Related
I'm sorry if the question already exist, but I didn't know what to look for.
I just need ideas for a solution.
so my problem is that I need to get different activities when I click cardview on recyclerview, but my cardview onclicklistener existing on my adapter and I don't know if or how i can check if the user is logged in.
i'll add my onclicklistener from my adapter.
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext, DetailActivity.class);
intent.putExtra("Image", myFoodList.get(holder.getAdapterPosition()).getItemImage());
intent.putExtra("Description", myFoodList.get(holder.getAdapterPosition()).getItemDescription());
intent.putExtra("keyValue",myFoodList.get(holder.getAdapterPosition()).getKey());
mContext.startActivity(intent);
}
});```
thank you for your help
Below is a demo of the idea:
I don't know what kind of authentication you use, but anyway in your activity that creates the adapter you should pass some flag that states if the user is signed in or not:
In your Activity:
class YourActivity .........{
private String loggedIn;
...........
//in onCreate()
//if user is logged in
loggedIn = "yes";
//else
loggedIn = "no";
//some where when you initialize the adapter
//pass the state
adapter = new MyAdapter(..........., loggedIn);
...........
}
In the Adapter:
class MyAdapter ........{
private String loggedIn;
......
.....
public MyAdapter(.........., String loggedIn){
.....
.....
.....
this.loggedIn = loggedIn;
}
When you click the item in the adapter:
#Override
public void onClick(View view) {
if(loggedIn.equals("yes")){
//logged in......
Intent intent = new Intent(mContext, DetailActivity.class);
intent.putExtra("Image", myFoodList.get(holder.getAdapterPosition()).getItemImage());
intent.putExtra("Description", myFoodList.get(holder.getAdapterPosition()).getItemDescription());
intent.putExtra("keyValue",myFoodList.get(holder.getAdapterPosition()).getKey());
mContext.startActivity(intent);
}else{
//not logged in open some other activity......
}
}
});
}
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 currently have a layout that has 4 horizontal RecyclerViews in succession. They all display how they are supposed to. However now I want to be able to click on a specific item in a RecyclerView and have it bring up more information. I have the click working with a single RecyclerView by overriding the onItemClick method. The only problem is I have 4 and I can't tell which RecyclerView is being clicked.
This is how I currently do it with one RecyclerView.
#Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(this, DetailedInfo.class);
intent.putExtra(DETAILED_INFO, customAdapter.getVideo(position));
startActivity(intent);
}
I have all of the RecyclerViews in a list so I can grab a specific one like this:
((VideoAdapter) recyclerViewList.get(1).getAdapter()).getVideo(position));
I just don't know how to find the one that was actually clicked.
you could do this inside adapter. pass your activity as context to your adapter.
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, DetailedInfo.class);
intent.putExtra(DETAILED_INFO, getVideo(position));
startActivity(intent);
}
});
}
I have my Cart Activity which contains the items that the user wanted to purchase.
What I want is if I click the "CHECK OUT" button, all of the data in the cart will be sent to the next Activity having ListView B.
Here is a part of my CartCustomerAdpater.class where i set the textview fields
final Order data = items.get(position);
holder.cart_name.setText(data.getName());
holder.cart_price.setText("Php "+data.getPrice());
holder.cart_qty.setText(data.getQty());
Here is a part of my Cart.class where my CHECK OUT button is located.
checkout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//WHAT SHOULD I DO HERE?
}
});
Suppose you declared your ListView:
ListView lv =(ListView) findViewById(R.id.lv);
For the case of a listView use setOnItemClickListener() , this is my sample code for sending data to the next Activity.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ModelClass jhild = (ModelClass) adapter.getItem(position);
Intent in = new Intent(getApplicationContext(), BuyApartmentsInformation.class);
in.putExtra(KEY_DISTRICT_NAME, jhild.getDistrict());
in.putExtra(KEY_FLOORS, jhild.getFloors());
in.putExtra(KEY_AREA, jhild.getArea());
in.putExtra(KEY_BUYLAND_CODE, jhild.getBuyno());
in.putExtra(KEY_PRICES, jhild.getPrice());
in.putExtra(KEY_INFORMATION, jhild.getInformation());
startActivity(in);
}
});
But my piece of Advice you have to update to RecyclerView
Hope it works well.
use intent
send
Intent intent = new Intent(A.this, B.class);
intent.putextra("keyName","value");
startActivity(intent);
recieve
String data = getIntent().getExtras().getString("keyName");
1. You need to implement Serializable or Parcelable in your **Order.java** class so that you can pass the ArrayList of Order through Bundle or Intent .
public class Order implements Serializable{
}
2. Pass the list of items through intent and start new activity
checkout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newActivity = new Intent(this , NewActivity.class);
newActivity.putExtra("orderList" , items);
startActivity(newActivity);
}
});
3. Get the list in NewActivity class
List<Order> itemsList = (List<Order>)getIntent.getSerializableExtra("orderList");
Hope it helps!
I have created a list view something like this: http://imgur.com/a/bhWhR in activity1
Onitemselect i want to launch another acitivity which shows description about listitem1 like below
http://imgur.com/a/agGma
but did not find any helpful material to create and activity with same layout to get different content like this one ::
I did explore for quite some time and I could not find any solution that helped me .
Use OnItemClickListener and use intent to open other activities on item clicks
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 1) {
Intent intent = new Intent(MainActivity.this, Lu1Activity.class);
startActivity(intent);
} else if (i == 2) {
Intent intent = new Intent(MainActivity.this, Lu2Activity.class);
startActivity(intent);
} else if (i == 3) {
Intent intent = new Intent(MainActivity.this, Lu3Activity.class);
startActivity(intent);
} else if (i == 4) {
Intent intent = new Intent(MainActivity.this, Lu4Activity.class);
startActivity(intent);
}
}
});
If you want to use same 2nd activity for each intent and show different content then use a global variable to know which item is selected and change content of 2nd activity as you wish.
Add a variable like this
public static int itemNumber;
Use OnItemClickListner
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
itemNumber = i;
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
});
In OtherActivity, Set different content to your views depending on itemNumber's value.
Note: You can also use putExtra to extend data to the intent and use that to determine which item is clicked.