Reputation: 21
I'm trying the last view days to open a Fragment by clicking on a recyclerview item. I'm still getting the same error: The method getSupportFragmentManager() is undefined for the type SightsAdapter.ListItemViewHolder
I read lots of threads but they couldn't help me and I'm absolutely new to android. Does anyone have any idea what i'm doing wrong?
Here is my code:
package com.example.stadtfuehrer;
import java.util.ArrayList;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.view.LayoutInflater;
import android.support.v4.app.*;
public class SightsAdapter extends RecyclerView.Adapter<SightsAdapter.ListItemViewHolder> {
private ArrayList<Sights> listData;
public SightsAdapter(ArrayList<Sights> list){
this.listData = list;
}
@Override
public int getItemCount(){
return listData.size();
}
@Override
public void onBindViewHolder(ListItemViewHolder holder, int position){
Sights sight = listData.get(position);
holder.textViewSightsName.setText(sight.getName());
}
@Override
public ListItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_recyclerview, parent, false);
//itemView.setPadding(10, 5, 10, 5);
return new ListItemViewHolder(itemView);
}
public static class ListItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textViewSightsName;
public ListItemViewHolder(View itemView){
super(itemView);
textViewSightsName = (TextView) itemView.findViewById(R.id.name_listitem);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v){
getSupportFragmentManager().beginTransaction().replace(R.id.container,new FragmentSightDetailed() ).commit();
Toast.makeText(itemView.getContext(), "Item clicked. "+textViewSightsName.getText(), Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 1
Views: 13990
Reputation: 2028
FragmentOne -->RecyclerViewAdapter onClcik() -->FragmentTwo
After a lot of searching i have solved this issue
FragmentOne fragmentOne;
public RecyclerViewAdapter(Context mContext, List<GetSetModel> list, FragmentOne fragmentOne) {
this.mContext = mContext;
this.list = list;
this.fragmentOne = fragmentOne;
}
**Now in OnClick() method open New Fragment like this below**
Fragment fragment = new FragmentTwo();
Bundle args = new Bundle();
args.putString("data", "This data has sent to FragmentTwo");
fragment.setArguments(args);
FragmentTransaction transaction = frgmentOne.getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(null);
transaction.commit();
Upvotes: 4
Reputation: 211
MainActivity main;
public ViewAdapter(Context context, List<Information> data,MainActivity ma)
{
inflater=LayoutInflater.from(context);
this.data=data;
this.context=context;
main=ma;
}
main.getSupportFragmentManager().beginTransaction().replace(R.id.container,new Fragment1()).commit();
This works fine for me,just pass MainActivity.this to the parameterised constructor of Adapter.It might be help for you.
Upvotes: 7