I need to pass some data of my adapter to bottom navigation view activity. Just like this:
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, RestaurantDetailsActivity.class);
intent.putExtra("restaurantUid", uid);
context.startActivity(intent);
}
});
I don't know where to put my button to use setOnClickListener
You should not change activities in Adapter. Rather, you should create an interface for your click listener and implement that in your Activity:
Create a Click Listener interface class ItemClickListenerInterface.Java
public interface ItemClickListener {
void onClick(View view, int position);
}
Then, in your viewHolder inside your AdapterClass, bind this click listener to the view Like this:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private ItemClickListener clickListener;
public void setClickListener(ItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (clickListener != null) clickListener.onClick(view, getAdapterPosition());
}
}
}
Then in Your ActivityClass.Java, implement this click list
public class CityActivity extends Activity implements ItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
mAdapter.setClickListener(this);
}
#Override
public void onClick(View view, int position) {
final Resturant resturant=resturantsList.get(position)
Intent intent = new Intent(context, RestaurantDetailsActivity.class);
intent.putExtra("restaurantUid", resturant.uid);
context.startActivity(intent)
}
}
Related
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//
}
});
Now, it is impossible to use startActivityForResult.
I have to use a launcher, but I don't know what to do because it's a click event that happens in the item view and I'm going to put the image in the item view
I tried various methods, but they were all for activities, so they didn't work.
You need either to cast the context to Activity, or to pass the onClickListener to the adapter as following
Create the clickListener
public interface MyClickListener {
void onItemClick(MyModel model);
}
In your adapter class:
public class MyAdapter extends RecyclerView.Adapter<ViewHolder> {
private MyClickListener onClickListener;
public MyAdapter(MyClickListener onClickListener) {
this.onClickListener = onClickListener;
}
...
}
In your method:
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onClickListener.onItemClick(myModel);
}
});
In your activity:
MyAdapter adapter = new MyAdapter(new MyClickListener() {
public void onItemClick(MyModel model) {
/// here you can call startActivityForResult
}
});
Below code of adapter of RecyclerView
public interface SelectMarket {
void selectMarket(DataMarket dataMarket);
}
public class viewHolder extends RecyclerView.ViewHolder{
TextView marketTitleName;
public viewHolder(#NonNull View itemView) {
super(itemView);
marketTitleName = itemView.findViewById(R.id.market_title_custom);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectMarket.selectMarket(marketName.get(getAdapterPosition()));
Toast.makeText(ctx, "id "+getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
}
}
In the code I shows you the ViewHolder
where I set onClickListener
I want to click on that and then go to other Fragment called SelectRoomFragment
and in Method of SelectMarket() I wrote inside the codes which will help me to go the the Next Fragment like this :
#Override
public void selectMarket(DataMarket dataMarket) {
SelectRoomFragment selectRoomFragment= new SelectRoomFragment();
Bundle args = new Bundle();
args.putString("market",dataMarket.getTitle());
selectRoomFragment.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.fragment_container,
selectRoomFragment).commit();
}
The Output Is
Which is not looking Good like I want.
move the code below to onBindViewHolder
marketTitleName = itemView.findViewById(R.id.market_title_custom);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectMarket.selectMarket(marketName.get(getAdapterPosition()));
Toast.makeText(ctx, "id "+getAdapterPosition(),Toast.LENGTH_SHORT).show();
}
});
I have multiple card views in my Main Fragment that go to different fragments within the app usin onClickListener functionality.
However, when the cardviews are selected the fragment that shows is always the last replaced.
Ex: In main activity all the card views, when selected, show the affirmations fragment. No matter if they have been connected to their proper fragment.
MainActivity.java
#Override
public void onCardSelected() {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_fragment, new ProfileFragment());
fragmentTransaction.replace(R.id.container_fragment, new BibleFragment());
fragmentTransaction.replace(R.id.container_fragment, new ShopFragment());
fragmentTransaction.replace(R.id.container_fragment, new NotiFragment());
fragmentTransaction.replace(R.id.container_fragment, new CalFragment());
fragmentTransaction.replace(R.id.container_fragment, new AffFragment());
fragmentTransaction.commit();
}
MainFragment.java
public class MainFragment extends Fragment {
private onFragmentBtnSelected listener;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_main,container,false);
CardView clickCardProfile = view.findViewById(R.id.cardProfile);
CardView clickCardBible = view.findViewById(R.id.cardBible);
CardView clickCardShop = view.findViewById(R.id.cardShop);
CardView clickCardNoti = view.findViewById(R.id.cardNotifications);
CardView clickCardCalendar = view.findViewById(R.id.cardCalendar);
CardView clickCardAffirmations = view.findViewById(R.id.cardAffirmations);
/*Button clickme = view.findViewById(R.id.clickHF);
clickme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onBtnSelected();
}
});
return view;*/
clickCardProfile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
//return view;
clickCardBible.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
//return view;
clickCardShop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
clickCardNoti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
clickCardCalendar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
clickCardAffirmations.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onCardSelected();
}
});
return view;
}
#Override
public void onAttach(#NonNull Context context){
super.onAttach(context);
if(context instanceof onFragmentBtnSelected ){
listener =(onFragmentBtnSelected) context;
}
else {
throw new ClassCastException(context.toString() + " must implement listiner");
}
}
public interface onFragmentBtnSelected{
//public void onBtnSelected();
public void onCardSelected();
}
Would I need to create an interface method for each of the buttons, and separate the respective replace statements or is there a way to have them all in the same method and switch from fragment to fragment?
I want to send in a variable the position of a click on an Item in a other activity. I show you my work that does not work. The application quits when I press an item.
MyViewHolder in my adapter:
public myViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get position
int poss = getAdapterPosition();
// sharing position
Intent intent = new Intent(mContext, Main2Activity.class);
intent.putExtra("position", poss );
mContext.startActivity(intent);
}
}
});
And my Activity for recept:
public class Main2Activity extends AppCompatActivity {
private TextView textTest;
int id = getIntent().getExtras().getInt("position");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
String position = String.valueOf(id);
textTest = findViewById(R.id.text_test);
textTest.setText(position);
}
Try
public class Main2Activity extends AppCompatActivity {
private TextView textTest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textTest = findViewById(R.id.text_test);
int id = getIntent().getExtras().getInt("position");
String position = String.valueOf(id);
textTest.setText(position);
}
Remove this line of code int id = getIntent().getExtras().getInt("position"); from outside of the onCreate() method & put this code inside of your onCreate Method in Main2Activity
textTest = findViewById(R.id.text_test);
int id = getIntent().getExtras().getInt("position");
String position = String.valueOf(id);
textTest.setText(position);
Create Interface class
Public interface OnPostionClick{
Public void yourmethod(int pos)
}
Place interface method on Adapter viewHolder onClick method
If(context instanceof OnPostionClick){
((OnPostionClick) context).yourmethod(poss);
}
implement OnPostionClick interface on Second Activity and override yourmethod
I'm trying, but when I implement both at the same time, the ViewHolder cannot reach none of those. Is it possible? My code:
public final class SomeViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener {
public final AestheticTextView mName;
public final AestheticTextView mNumber;
public SomeViewHolder(View itemView) {
super(itemView);
mName = (AestheticTextView) itemView.findViewById(android.R.id.title);
mNumber = (AestheticTextView) itemView.findViewById(android.R.id.summary);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
#Override
public void onClick(View view) {
// doesn't run
}
#Override
public boolean onLongClick(View view) {
// doesn't run
}
}
If I remove one of the listeners, like below, it works fine.
public final class SomeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final AestheticTextView mName;
public final AestheticTextView mNumber;
public SomeViewHolder(View itemView) {
super(itemView);
mName = (AestheticTextView) itemView.findViewById(android.R.id.title);
mNumber = (AestheticTextView) itemView.findViewById(android.R.id.summary);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
// run properly
}
}
Create a bind method or whatever you want to call it.
public void bind(ModelClass model, MyInterface listener) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Create your own interface and add arguments based on your needs
listener.itemClicked(view);
}
});
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Create your own interface and add arguments based on your needs
listener.itemLongClicked(view);
}
});
}
On your onBindViewHolder()
holder.bind(item.get(position), listener);
And when declaring your Adapter
adapter = new MyAdapter(item, new OnItemClick() {
#Override
public void itemClicked(View view, ModelClass model) {
//event
}
#Override
public void itemLongClicked(View view, ModelClass model) {
return true;
}
});