I can't put my textView in the method onBindViewHolder from the inner class Item, I made it public and final and is still cannot resolve it, I tried many examples and it's the same problem.
package com.example.recyclerview;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
String [] items;
public Adapter(Context context, String[] items){
this.context = context;
this.items = items;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(context);
View row = inflater.inflate(R.layout.custom_row, viewGroup, false);
Item item = new Item(row);
return item;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int i) {
String mCurrent = items[i];
viewHolder.textView.setText(mCurrent); // error here textView cannot resolve
}
#Override
public int getItemCount() {
return items.length;
}
public class Item extends RecyclerView.ViewHolder {
public final TextView textView;
public Item(#NonNull View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.item);
}
}
}
You should correct generic type of the adapter: use Adapter.Item instead of RecyclerView.ViewHolder
public class Adapter extends RecyclerView.Adapter<Adapter.Item>
and then update signature of onCreateViewHolder and onBindViewHolder methods
Related
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import com.recipi.myapprishi.Model.RecipieModel;
import com.recipi.myapprishi.R;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class Recipe_Adapter extends RecyclerView.Adapter<Recipe_Adapter.viewHolder> {
ArrayList<RecipieModel> list;
Context context;
public Recipe_Adapter(ArrayList<RecipieModel> list, Context context) {
this.list = list;
this.context = context;
}
#NonNull
#Override
public viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//this line have error indicated by logcat
View view = LayoutInflater.from(context).inflate(R.layout.sample_recyclerview , null);
//this line have error indicated by logcat
return new viewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull viewHolder holder, int position) {
RecipieModel model = list.get(position);
holder.imageView.setImageResource(model.getPic());
holder.textView.setText(model.getText());
}
#Override
public int getItemCount() {
return list.size();
}
public class viewHolder extends RecyclerView.ViewHolder {
public ImageSwitcher imageView;
ImageView imageview;
TextView textView;
public viewHolder(#NonNull View itemView) {
super(itemView);
imageview = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
}
}
}
what's wrong in this code it giving me run time error like unfortunately app has been stopped and logcat is indicating me on bolded line
Error :
at com.recipi.myapprishi.Adapter.Recipe_Adapter.onCreateViewHolder(Recipe_Adapter.java:32)
at com.recipi.myapprishi.Adapter.Recipe_Adapter.onCreateViewHolder(Recipe_Adapter.java:16)
You can inflate your view in this way:
#NonNull
#Override
public viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContet()).inflate(R.layout.sample_recyclerview , parent, false);
return new viewHolder(view);
}
Also, verify if R.layout.sample_recyclerview is the correct parameter.
I am trying to attach data in view.But I here show error on OnBind method.
Here is the code of mainActivity:
package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
int [] image = {
R.drawable.deep_blue,R.drawable.dual_matter,R.drawable.gradiant,
R.drawable.light_blue,R.drawable.yellow,R.drawable.red
};
MyAdapter myAdapter;
String [] title,desc;
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.RecyclerViewID);
title=getResources().getStringArray(R.array.logo_name);
desc=getResources().getStringArray(R.array.logo_desc);
myAdapter =new MyAdapter(this,title,desc,image);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
Here is the code of Adapter class:
package com.example.recyclerview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyAdapterHolder> {
Context context;
String [] title;
String [] desc;
int [] image;
public MyAdapter(Context context, String[] title, String[] desc, int[] image) {
this.context = context;
this.title = title;
this.desc = desc;
this.image = image;
}
#NonNull
#Override
public MyAdapterHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view=layoutInflater.inflate(R.layout.sampleview,viewGroup,false);
return new MyAdapterHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyAdapterHolder myAdapterHolder, int position) {
myAdapterHolder.titleTV.setText(title[i]);
myAdapterHolder.descTV.setText(desc[i]);
myAdapterHolder.myimageView.setImageResource(image[i]);
}
#Override
public int getItemCount() {
return title.length;
}
public class MyAdapterHolder extends RecyclerView.ViewHolder {
TextView titleTV,descTV;
ImageView myimageView;
public MyAdapterHolder(#NonNull View itemView) {
super(itemView);
titleTV=itemView.findViewById(R.id.titleTextViewID);
descTV=itemView.findViewById(R.id.descTextViewID);
myimageView=itemView.findViewById(R.id.myImageViewID);
}
}
}
#Override
public void onBindViewHolder(#NonNull MyAdapterHolder myAdapterHolder, int position) {
myAdapterHolder.titleTV.setText(title[i]);
myAdapterHolder.descTV.setText(desc[i]);
myAdapterHolder.myimageView.setImageResource(image[i]);
}
As far as I can tell you are trying to use position for your index, but you are instead using the variable i. Try changing the i 's in this function to position. If you could post your stack trace that would help a bunch too.
RecyclerView Code:OnClickListener
package com.example.cardview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class ArrayAdapter extends RecyclerView.Adapter<ArrayAdapter.ViewHolder> {
private String[] divisionName;
ArrayAdapter(String[] phoneNumebr) {
this.divisionName = phoneNumebr;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from( context );
View v = inflater.inflate( R.layout.element, parent, false );
return new ViewHolder( v );
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
holder.cNumbers.setText(divisionName[position] );
holder.itemView.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(this,divisionName[position],Toast.LENGTH_LONG).show();
}
} );
}
#Override
public int getItemCount() {
return divisionName.length;
}
static class ViewHolder extends RecyclerView.ViewHolder{
TextView cNumbers;
ViewHolder(#NonNull View itemView) {
super( itemView );
this.cNumbers = itemView.findViewById( R.id.cNumbers );
}
How to add OnClickListener in recyclerView according to the item position
How to add OnClickListener in recyclerView according to the item
position
To add OnClickListener to RecyclerView items, you achieve that within your custom RecyclerView.ViewHolder class, by setting .onClickListener() to the View passed into the ViewHolder constructor
snippet from your code:
static class ViewHolder extends RecyclerView.ViewHolder{
TextView cNumbers;
ViewHolder(#NonNull View itemView) {
super( itemView );
this.cNumbers = itemView.findViewById( R.id.cNumbers );
listItem.setOnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Click on position number: " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
Toast.makeText(v.getContext(), divisionName[getAdapterPosition()],Toast.LENGTH_LONG).show();
}
}
}
}
Just Change Your Toast.
Toast.makeText(holder.itemView.getContext(),"You clicked "+divisionName[position],Toast.LENGTH_SHORT).show();
I am trying to open a dialog box from my Recycler adapter onClick of a button.
For that i need to get the FragmentManager using the getFragmentManager method ,which can only be called with the help of the activity object .
Here is my code :
RecyclerAdapter2 :
package com.example.batrad.expenseassist;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Collections;
import java.util.List;
/**
* Created by batrad on 11/18/2016.
*/
public class RecyclerAdapter2 extends RecyclerView.Adapter<RecyclerAdapter2.MyViewHolder> {
List<Information2> data = Collections.emptyList();
private LayoutInflater inflator;
Activity activity;
FragmentManager fm;
public RecyclerAdapter2(Context context, List<Information2> data) {
activity=(Activity)context;
inflator = LayoutInflater.from(context);
this.data = data;
fm=activity.getFragmentManager();
}
#Override
public RecyclerAdapter2.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view =inflator.inflate(R.layout.recycler_row_category,parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(RecyclerAdapter2.MyViewHolder holder, int position) {
Log.d("tag",data.get(0)+"");
Information2 info=data.get(position);
Log.d("testre",info.categoryName+info.amount);
holder.categoryName.setText(info.categoryName);
holder.amount.setText(info.amount+"");
holder.categoryImage.setImageResource(info.categoryImage);
}
public void onInsert(int size){
notifyItemInserted(size);
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView categoryName;
TextView amount;
ImageView categoryImage;
ImageView deleteIcon;
ImageView addIcon;
public MyViewHolder(View itemView) {
super(itemView);
categoryName=(TextView) itemView.findViewById(R.id.categoryName);
amount=(TextView) itemView.findViewById(R.id.amount);
categoryImage=(ImageView) itemView.findViewById(R.id.categoryImage);
deleteIcon=(ImageView) itemView.findViewById(R.id.deleteIcon);
addIcon=(ImageView) itemView.findViewById(R.id.addIcon);
addIcon.setOnClickListener(this);
}
#Override
public void onClick(View view) {
SubCategoryDialog subCategoryDialog=new SubCategoryDialog();
SubCategoryDialog.setValues(data.get(getAdapterPosition()).categoryName);
subCategoryDialog.show(fm, "Create Category");
}
}
}
Inside the constructor of RecyclerAdapter2 I am passing the context of the activity from my MainActivity Java file:
MainActivity.java :
categoryrecycleAdapter = new RecyclerAdapter2(this, getdata());
Inside RecyclerAdapter2 constructor i assign the context to Activity class's object .
And than i use the object to get the fragmentManager.
But i am getting IllegalStateException here :
java.lang.IllegalStateException: Activity has been destroyed
at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1433)
at android.app.BackStackRecord.commitInternal(BackStackRecord.java:687)
at android.app.BackStackRecord.commit(BackStackRecord.java:663)
at android.app.DialogFragment.show(DialogFragment.java:230)
at com.example.batrad.expenseassist.RecyclerAdapter2$MyViewHolder.onClick(RecyclerAdapter2.java:85)
at android.view.View.performClick(View.java:5233)
at android.view.View$PerformClick.run(View.java:21209)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:152)
Please help .
This is not the right way to attach listeners. You should set the onClickListener in the constructor of your viewholder and should send a callback to your fragment using an interface as shown below or you can also use BaseAdapter from this or this.
public interface OnItemClickListener {
void onItemClick(View v);
}
private final OnItemClickListener listener;
public RecAdapter(OnItemClickListener listener) {
this.listener = listener;
}
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemCLick(v);
}
});
}
Reading old questions I have and watching several tutorials I have written this code.
RecyclerAdapter.java
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
String[] postText;
String[] groupName;
public RecyclerAdapter(String[] postText, String[] groupName){
this.postText = postText;
this.groupName = groupName;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.txPost.setText(postText[position]);
holder.txGroupName.setText(groupName[position]);
holder.cardView.setCardBackgroundColor(Color.GREEN);
}
#Override
public int getItemCount() {
return postText.length;
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView txPost;
TextView txGroupName;
CardView cardView;
public RecyclerViewHolder(View view)
{
super(view);
txPost = (TextView) view.findViewById(R.id.post_text);
cardView = (CardView) itemView.findViewById(R.id.card_view);
txGroupName = (TextView) view.findViewById(R.id.group_name);
}
}
}
This works fine but now I need to change the colors randomly. I tried to pass an array string array of colors to the holder.cardView.setCardBackgroundColor(Color.GREEN); but it threw an error because it takes int. I am new to java and programming as a whole. What is the proper way of doing it?