I would like to pass data from adapter to my fragment, so basically when i try to click an item, The fragment should open, but, my app did nothing. so how can i solve this?
Adapter
public class CatAdapter extends RecyclerView.Adapter<CatAdapter.ViewHolder> {
private static String TAG = "Cat Adapter";
private ArrayList<String> catUrlImage = new ArrayList<>();
private ArrayList<String> cuteCatName = new ArrayList<>();
private Context catContext;
private FragmentManager fragmentManager;
private OnRecyclerViewItemClickListener listenerthis;
public CatAdapter(ArrayList<String> catUrlImage, ArrayList<String> cuteCatName, Context catContext, OnRecyclerViewItemClickListener listener) {
this.catUrlImage = catUrlImage;
this.cuteCatName = cuteCatName;
this.catContext = catContext;
this.listenerthis = listener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item, viewGroup, false);
ViewHolder catViewHolder = new ViewHolder(v);
return catViewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
Log.d(TAG, "OnBinding: called");
Glide.with(catContext)
.asBitmap()
.load(catUrlImage.get(position))
.into(viewHolder.catImage);
viewHolder.catName.setText(cuteCatName.get(position));
viewHolder.catItemLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(catContext, cuteCatName.get(position), Toast.LENGTH_SHORT).show();
listenerthis.onClick(position);
}
});
}
@Override
public int getItemCount() {
return catUrlImage.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView catImage;
TextView catName;
RelativeLayout catItemLayout;
public ViewHolder(View itemView) {
super(itemView);
catImage = itemView.findViewById(R.id.iLikeCat);
catName = itemView.findViewById(R.id.catVeryCuteName);
catItemLayout = itemView.findViewById(R.id.cat_container);
}
}
public interface OnRecyclerViewItemClickListener{
void onClick(int parameter);
}
}
i already change my Activity before the post so it more changing the onRecycleViewOnItemClickListener
MainActivity
public class MainActivity extends BaseActivity implements CatAdapter.OnRecyclerViewItemClickListener{
private static final String TAG = "MainActivity";
private ArrayList<String> catArray = new ArrayList<>();
private ArrayList<String> catArrayImageUrl = new ArrayList<>();
DataCat dataCat = new DataCat();
private RecyclerView recyclerView;
private RecyclerView.Adapter rAdapter;
private RecyclerView.LayoutManager rLayoutManager;
GalleryFragment galleryFragment = new GalleryFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Cat Has Create OnCreated");
findView();
initView();
catData();
}
private void catData() {
Log.d(TAG, "Preparing A Bitmap for a Cat");
for (int i = 0; i < dataCat.catUrl.length; i++) {
catArrayImageUrl.add(dataCat.catUrl[i]);
catArray.add(dataCat.catName[i]);
}
initRecycleView();
}
private void initRecycleView() {
CatAdapter catAdapter = new CatAdapter(catArrayImageUrl, catArray, this, this);
recyclerView.setAdapter(catAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void findView() {
Log.d(TAG, "initRecylce Vieq");
recyclerView = findViewById(R.id.recyclerViewList);
rLayoutManager = new LinearLayoutManager(this);
}
@Override
public void initView() {
}
@Override
public void initListener() {
recyclerView.setHasFixedSize(true);
}
@Override
public void onClick(int parameter) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_galery, galleryFragment)
.addToBackStack(null)
.commit();
}
}
I input some layout maybe there some bad structure on my layout
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<FrameLayout
android:id="@+id/container_galery"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
i've already try the solutions from the comment bellow and my fragment still not showing up. I didnt get any error from my logCat, when i try to click.
Do two small changes in your code-
Adapter
public CatAdapter(ArrayList<String> catUrlImage, ArrayList<String> cuteCatName, Context catContext, OnRecyclerViewItemClickListener listenerthis) {
this.catUrlImage = catUrlImage;
this.cuteCatName = cuteCatName;
this.catContext = catContext;
this.listenerthis = listenerthis;
}
MainActivity
private void initRecycleView() {
CatAdapter catAdapter = new CatAdapter(catArrayImageUrl, catArray, this, this);
recyclerView.setAdapter(catAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
thats it. Hope it will be helpful.