I can not set Set Text in Card View name and surname - java

I have problem with set text in card view. I have 3 activist. First activity is list , second activity which show edit text which I complete data Person next acttivty 3 summary click buton go to MainActivty. When click to MainActitvty display error.
04-05 09:21:06.879 1035-1035/magdalena.pl.callmistake E/AndroidRuntime: FATAL EXCEPTION: main
Process: magdalena.pl.callmistake, PID: 1035
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String magdalena.pl.callmistake.Person.getName()' on a null object reference
at magdalena.pl.callmistake.PersonAdapter.onBindViewHolder(PersonAdapter.java:41)
at magdalena.pl.callmistake.PersonAdapter.onBindViewHolder(PersonAdapter.java:18)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
my PersonAdapter
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonViewHolder> {
public List<Person> personList = new ArrayList<>();
Person person;
private LayoutInflater layoutInflater;
public PersonAdapter(LayoutInflater layoutInflater) {
this.layoutInflater = layoutInflater;
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.
from(parent.getContext()).inflate(R.layout.item_card, parent, false);
return new PersonViewHolder(view);
}
#Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
person = personList.get(position);
holder.name.setText(person.getName());
holder.surname.setText(person.getSurname());
}
#Override
public int getItemCount() {
return personList.size();
}
public void addPerson(Person person) {
int position = getItemCount();
personList.add(position, person);
notifyDataSetChanged();
}
public class PersonViewHolder extends RecyclerView.ViewHolder {
public TextView name, surname;
public PersonViewHolder(View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.person_name);
surname = (TextView)itemView.findViewById(R.id.person_surname);
}
}
}
class Person
public class Person implements Parcelable
{
private String name;
private String surname;
private String email;
private String phone;
private String description;
protected Person(Parcel in) {
name = in.readString();
surname = in.readString();
email = in.readString();
phone = in.readString();
description = in.readString();
}
public Person(String name, String surname, String email, String phone, String description) {
this.name = name;
this.surname = surname;
this.email = email;
this.phone = phone;
this.description = description;
}
public String setName(String name) {
this.name = name;
return null;
}
public String getSurname() {
return surname;
}
public String setSurname(String surname) {
this.surname = surname;
return null;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public static Creator<Person> getCREATOR() {
return CREATOR;
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
#Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
#Override
public Person[] newArray(int size) {
return new Person[size];
}
};
public String getName() {
return name;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(surname);
parcel.writeString(email);
parcel.writeString(phone);
parcel.writeString(description);
}
}
What is wrong ?
all code
https://github.com/Madzia123/CallInert

Apparently, In the onBindViewHolder method the person object is null. And you are trying to invoke getName() method on null reference. So it is causing the error. Make sure to have valid reference to Person object.

You are adding null person to personList at
public void addPerson(Person person) {
int position = getItemCount();
personList.add(position, person);
notifyDataSetChanged();
}
That's why when you call
person = personList.get(position);
it returns null
try this
public void addPerson(Person person) {
if(person==null){
Log.e("error","person is null");
return;
}
int position = getItemCount();
personList.add(position, person);
notifyDataSetChanged();
}

final Person person = personList.get(position);
holder.name.setText(person.getName());

From your attached logs, seems that you are getting NullPointerException when calling Person.getName() from your adapter's onBindViewHolder().
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String magdalena.pl.callmistake.Person.getName()' on a null
object reference
Solution:
Update your PersonAdapter as below:
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonViewHolder> {
Context context;
private LayoutInflater layoutInflater;
// Person List
public List<Person> personList;
public PersonAdapter(Context context, List<Person> persons) {
this.context = context;
this.personList = persons;
layoutInflater= LayoutInflater.from(context);
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.item_card, parent, false);
// View holder
PersonViewHolder holder = new PersonViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
// Person
Person person = personList.get(position);
holder.name.setText(person.getName());
holder.surname.setText(person.getSurname());
}
#Override
public int getItemCount() {
return personList.size();
}
public void addPerson(Person person) {
int position = getItemCount();
personList.add(position, person);
notifyDataSetChanged();
}
public class PersonViewHolder extends RecyclerView.ViewHolder {
public TextView name, surname;
public PersonViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.person_name);
surname = (TextView) itemView.findViewById(R.id.person_surname);
}
}
}
In your Activity do this:
..........
.................
Context mContext;
// RecyclerView
RecyclerView recyclerView;
PersonAdapter personAdapter;
RecyclerView.LayoutManager layoutManager;
// Values
List<Person> listPerson;
#Override
protected void onCreate(Bundle savedInstanceState) {
.......
............
// Context
mContext = this;
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
// Set your recyclerView layout manager
// Person List
listPerson = new ArrayList<Person>();
// Add some person data to list from API call or from Database
// Here I added some for test purpose
listPerson.add(new Person("Hello Android", "Android", "google#gmail.com", "21132342423", "Lorem ipsum"));
listPerson.add(new Person("Hello Android", "Android", "google#gmail.com", "21132342423", "Lorem ipsum"));
// specify an adapter
personAdapter = new PersonAdapter(mContext, listPerson);
recyclerView.setAdapter(personAdapter);
}
...............
...........................
Hope this will help you~

first pass the data from where you call this adapter.
adapter = new PersonAdapter(getContext(),(ArrayList<Person>) list);
use arraylist.method to access class method.
holder.name.setText(person.personList(position).getName());

Related

Error while displaying data from Firebase Database to RecyclerView

We have been facing this error while our app tries to display data from our Firebase Database. Here is the error shown in logcat:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.projectrefill.retailer_model_datewise_dispwhenpressed
Here is the screenshot of the database where we are trying to display data from
Retailer> Kamath Bakery> r_history > (date_with_time) > (number)> :
Java Class (retailerside_datewisetransaction_Fragment) :
FirebaseRecyclerOptions<retailer_model_datewise_dispwhenpressed> options =
new FirebaseRecyclerOptions.Builder<retailer_model_datewise_dispwhenpressed>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Retailer").child(username).child("r_history").child(datenew),retailer_model_datewise_dispwhenpressed.class)
.build();
adapter=new adapter_retailerside_datewise_dispoforder(options);
adapter.startListening();
recyclerView.setAdapter(adapter);
Model Class (retailer_model_datewise_dispwhenpressed) :
public class retailer_model_datewise_dispwhenpressed {
String name,price,quan,totalamount,weight;
public retailer_model_datewise_dispwhenpressed() {
}
public retailer_model_datewise_dispwhenpressed(String name, String price, String quan, String totalamount, String weight) {
this.name = name;
this.price = price;
this.quan = quan;
this.totalamount = totalamount;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getQuan() {
return quan;
}
public void setQuan(String quan) {
this.quan = quan;
}
public String getTotalamount() {
return totalamount;
}
public void setTotalamount(String totalamount) {
this.totalamount = totalamount;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
Adapter Class (adapter_retailerside_datewise_dispoforder) :
public class adapter_retailerside_datewise_dispoforder extends FirebaseRecyclerAdapter<retailer_model_datewise_dispwhenpressed,adapter_retailerside_datewise_dispoforder.myviewholder> {
public adapter_retailerside_datewise_dispoforder(#NonNull FirebaseRecyclerOptions<retailer_model_datewise_dispwhenpressed> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myviewholder holder, int position, #NonNull retailer_model_datewise_dispwhenpressed model) {
holder.name.setText("Name: "+model.getName());
holder.price.setText("Price: "+model.getPrice());
holder.quan.setText("Quan: "+model.getQuan());
holder.totprice.setText(model.getTotalamount());
holder.weight.setText("Weight: "+model.getWeight());
}
#NonNull
#Override
public myviewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_retailer_datewise_whenpressed,parent,false);
return new myviewholder(view);
}
public class myviewholder extends RecyclerView.ViewHolder {
TextView name,price,quan,weight,totprice;
public myviewholder(#NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.datewisename);
price=itemView.findViewById(R.id.datewiseprice);
quan=itemView.findViewById(R.id.datewisequan);
weight=itemView.findViewById(R.id.datewiseweight);
totprice=itemView.findViewById(R.id.totalpricehere);
}
}
}
Since you pass this path to the FirebaseUI adapter:
FirebaseDatabase.getInstance().getReference().child("Retailer").child(username).child("r_history").child(datenew)
Firebase will read each child node under that path and try to create a retailer_model_datewise_dispwhenpressed object out of it.
This works fine for the child 1, as it has the properties that your class has. But then Firebase tries to do the same for Pmode, and that is just a string value, which is not a valid retailer_model_datewise_dispwhenpressed object.
You'll have to ensure that you only have child nodes that are valid retailer_model_datewise_dispwhenpressed objects under the path that you load.

Firestore using recyclerview I want to append a picture

I want to show people's information including a picture.
Here is User.class
public class User {
//파이어 스토어 필드 변수명과 타입이 같아야 한다. ex) 프사 라즈 INTP 구일 23
String name, mbti, region, age, profile_image;
public User(){
//must be empty
}
public User(String name, String mbti, String region, String age, String profile_image) {
this.name = name;
this.mbti = mbti;
this.region = region;
this.age = age;
this.profile_image = profile_image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMbti() { return mbti; }
public void setMbti(String mbti) {this.mbti = mbti; }
public String getRegion() { return region; }
public void setRegion(String region) { this.region = region; }
public String getAge() { return age; }
public void setAge(String age) { this.age = age; }
public String getProfile_image() { return profile_image; }
public void setProfile_image(String profile_image) { this.profile_image = profile_image; }}
And here is the MyAdapter class
Context context;
ArrayList<User> userArrayList;
public MyAdapter(Context context, ArrayList<User> userArrayList) {
this.context = context;
this.userArrayList = userArrayList;
}
#NonNull
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder holder, int position) {
User user = userArrayList.get(position);
holder.name.setText(user. name);
holder.mbti.setText(user.mbti);
holder.region.setText(user.region);
holder.age.setText(user.age);
Glide.with(context).load(user.getProfile_image()).into(holder.imgprofile);
}
#Override
public int getItemCount() {
return userArrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView name, mbti, region, age;
ImageView imgprofile;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.tvfirstName);
mbti = itemView.findViewById(R.id.item_mbti);
region = itemView.findViewById(R.id.item_region);
age = itemView.findViewById(R.id.item_age);
imgprofile = itemView.findViewById(R.id.main_profileimg);
}
}
In my MainActivity I use firestore.collection() and userArrayList.add(dc.getDocument().toObject(User.class)) So recyclerview can have people's information.
But Pictures are stored in Firebase storage -> profile/pics -> uid.jpg..
I'm beginner so I can't transform code userArrayList.add(dc.getDocument().toObject(User.class))
firestore.collection("Users")
.whereEqualTo("mbti",mainsearch_mbti)
.orderBy("time",Query.Direction.DESCENDING).limit(50)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot value, #Nullable FirebaseFirestoreException error) {
if(error != null){
if(progressDialog.isShowing()){
progressDialog.dismiss();
}
Log.e("Firestore error",error.getMessage());
return;
}
for (DocumentChange dc : value.getDocumentChanges()){
if (dc.getType() == DocumentChange.Type.ADDED){
userArrayList.add(dc.getDocument().toObject(User.class));
}
myAdapter.notifyDataSetChanged();
if(progressDialog.isShowing()){
progressDialog.dismiss();
}
}
}
});
You can take data with value.getDocuments() function.
for (DocumentSnapshot snapshot : value.getDocuments()) {
Map<String,Object> dataMap = snapshot.getData();
String abc = (String) dataMap.get("abc");
}

Receiving null Parcelable object Android

I'm trying to pass an object from my RecyclerView adapter to a fragment using parcelable. However when I try to receive the data, the bundle is null. I've looked at other examples, but I can't see where I'm going wrong.
Parcelable class
public class Country extends BaseObservable implements Parcelable {
#SerializedName("name")
#Expose
private String name;
#SerializedName("snippet")
#Expose
private String snippet;
#SerializedName("country_id")
#Expose
private String countryId;
#SerializedName("id")
#Expose
private String id;
#SerializedName("coordinates")
#Expose
private Coordinates coordinates;
#SerializedName("images")
#Expose
private List<CountryImage> images;
protected Country(Parcel in) {
name = in.readString();
snippet = in.readString();
}
public static final Creator<Country> CREATOR = new Creator<Country>() {
#Override
public Country createFromParcel(Parcel in) {
return new Country(in);
}
#Override
public Country[] newArray(int size) {
return new Country[size];
}
};
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(snippet);
}
#Override
public int describeContents() {
return 0;
}
#Bindable
public String getCountryId() {
return countryId;
}
public void setCountryId(String countryId) {
this.countryId = countryId;
notifyPropertyChanged(BR.countryId);
}
#Bindable
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
notifyPropertyChanged(BR.id);
}
#Bindable
public Coordinates getCoordinates() {
return coordinates;
}
public void setCoordinates(Coordinates coordinates) {
this.coordinates = coordinates;
notifyPropertyChanged(BR.coordinates);
}
#Bindable
public List<CountryImage> getImages() {
return images;
}
public void setImages(List<CountryImage> images) {
this.images = images;
notifyPropertyChanged(BR.images);
}
#Bindable
public String getSnippet() {
return snippet;
}
public void setSnippet(String snippet) {
this.snippet = snippet;
notifyPropertyChanged(BR.snippet);
}
#Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
}
RetrofitAdapter.java
public class RetrofitAdapter extends RecyclerView.Adapter<RetrofitAdapter.MyViewHolder> implements CustomClickListener {
private List<Country> cities;
private CustomClickListener customClickListener;
private View view;
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
RetroItemBinding retroItemBinding =
DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()),
R.layout.retro_item, viewGroup, false);
view = viewGroup;
return new MyViewHolder(retroItemBinding);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.bindTo(cities.get(i));
myViewHolder.retroItemBinding.setItemClickListener(this);
}
#Override
public int getItemCount() {
if (cities != null) {
return cities.size();
} else {
return 0;
}
}
public void setCityList(ArrayList<Country> cities) {
this.cities = cities;
notifyDataSetChanged();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private RetroItemBinding retroItemBinding;
public MyViewHolder(#NonNull RetroItemBinding retroItemBinding) {
super(retroItemBinding.getRoot());
this.retroItemBinding = retroItemBinding;
}
void bindTo(Country country) {
retroItemBinding.setVariable(BR.city, country);
retroItemBinding.setVariable(BR.itemClickListener, customClickListener);
retroItemBinding.executePendingBindings();
}
}
public void cardClicked(Country country) {
CountryFragment countryFragment = new CountryFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("Country", country);
countryFragment.setArguments(bundle);
((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction()
.replace(R.id.frag_container, new CountryFragment())
.commit();
}
}
Where I receive attempt to receive the data in CountryFragment.java
Country country;
Bundle bundle = this.getArguments();
if (bundle != null) {
country = bundle.getParcelable("Country");
}
.replace(R.id.frag_container, new CountryFragment())
should be
.replace(R.id.frag_container, countryFragment)
You are creating a second instance instead of passing the one you set the arguments on.

RecyclerView doesn't display all items in list after update android studio

I want to show these items inside my RecyclerView but it doesn't show at all and I can't see the error. Maybe you guys can help me out.
My code here:
The Activity:
public class ProductsViewActivity extends AppCompatActivity {
private RecyclerView productView;
private ArrayList<Product> lstProduct;
private RecyclerView.LayoutManager layoutManager;
private ProductAdapter productAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products_view);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
lstProduct = new ArrayList<>();
lstProduct.add(new Product(1, "Product 1", "This is good product", "https://www.dollargeneral.com/media/catalog/product/cache/image/700x700/e9c3970ab036de70892d86c6d221abfe/2/0/20706301_kibbles_nbitsbacon_steakflavordrydogfood3.6lb_main.jpg", 34.78));
lstProduct.add(new Product(2, "Product 2", "This is good product", "https://www.iams.com/images/default-source/product-packshot/dog/iams-proactive-health-smart-puppy-large-breed-dog-food.png?sfvrsn=6", 34.78));
productView = findViewById(R.id.productView);
loadProductView();
}
private void loadProductView() {
productView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
productView.setLayoutManager(layoutManager);
productAdapter = new ProductAdapter(lstProduct);
productView.setAdapter(productAdapter);
Log.e("Product view", "Size ======>" + productAdapter.getItemCount());
}
}
The Adapter class:
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.RecycleViewHolder> {
public static class RecycleViewHolder extends RecyclerView.ViewHolder {
private TextView tvName;
private TextView tvDescription;
private TextView tvPrice;
private TextView tvImage;
public RecycleViewHolder(#NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tvName);
tvDescription = itemView.findViewById(R.id.tvDescription);
tvPrice = itemView.findViewById(R.id.tvPrice);
tvImage = itemView.findViewById(R.id.tvImage);
}
}
private List<Product> lstProduct;
public ProductAdapter(List<Product> lstProduct) {
this.lstProduct = lstProduct;
}
#NonNull
#Override
public RecycleViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.product_list_item, parent, false);
RecycleViewHolder viewHolder = new RecycleViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RecycleViewHolder holder, int position) {
holder.tvName.setText(lstProduct.get(position).getName());
holder.tvDescription.setText(lstProduct.get(position).getDescription());
holder.tvPrice.setText(String.valueOf(lstProduct.get(position).getPrice()));
holder.tvImage.setText(String.valueOf(lstProduct.get(position).getImgURL()));
}
#Override
public int getItemCount() {
return lstProduct.size();
}
}
The Product class:
public class Product implements Serializable {
private int id;
private String name;
private String description;
private String imgURL;
private double price;
public Product() {
}
public Product(int id, String name, String description, String imgURL, double price) {
this.id = id;
this.name = name;
this.description = description;
this.imgURL = imgURL;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImgURL() {
return imgURL;
}
public void setImgURL(String imgURL) {
this.imgURL = imgURL;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
check if "product_list_item" root ViewGroup width is : wrap_content not match_parent.
layou/product_list_item.xml:
<LinearLayout
android:layout_height="wrap_content"
of course you encounter this problem if it was match_parent.
Seems all good, But one thing try to invalidate cache and restart your android studio
File > Invalidate caches/Restart
Hope so, it will work

set boolean value to Realm database when checkbox is checked

I have a realm database model which i want to display in arecyclerview,
every row in the database model have a String name,int age and a boolean favourite,when i try to save the boolean value in the RecyclerviewAdapter
by using an interface but i gjava.lang.NullPointerException: Attempt to invoke virtual method 'void io.realm.Realm.beginTransaction()' on a null object reference
this the model
public class person extends RealmObject implements{
#PrimaryKey
private String name;
private String job;
private boolean favourite;
private String image;
public AuthorInfo(String image, String job, String name, boolean favoutite) {
this.image = image;
this.job = job;
this.name = name;
this.favourite = favourite;
}
public AuthorInfo() {
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFavourite() {
return favourite;
}
public void setFavourite(boolean favourite) {
this.favourite = favourite;
}
}
this is the interface
public interface ItemClickListener {
void onItemClick(View view,int position);}
and this the recyclerview adapter
public class PersonAdapter extends RecyclerView.Adapter<AuthorAdapter.MyViewHolder> {
private LayoutInflater inflater;
private Realm mRealm;
private RealmResults<AuthorInfo> results;
public PersonAdapter(Context context, RealmResults<AuthorInfo> realmResults) {
inflater = LayoutInflater.from(context);
results = realmResults;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
AuthorInfo current = results.get(position);
holder.name.setText(current.getName());
holder.job.setText(current.getJob());
holder.setClickListener(new ItemClickListener() {
mRealm.beginTransaction();
#Override
public void onItemClick(View view, int position) {
CheckBox chk= (CheckBox) view;
if (chk.isChecked()){
results.get(position).setFavourite(true);
}
else if (!chk.isChecked()){
results.get(position).setFavourite(false);
}
}
mRealm.commitTransaction();
notifyItemChanged(position)
});
}
#Override
public int getItemCount() {
return results.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView name;
TextView job;
CheckBox checkbox;
ItemClickListener itemclicklistener;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.tv_name);
job = (TextView) itemView.findViewById(R.id.tv_job);
checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
checkbox.setOnClickListener(this);
}
public void setClickListener(ItemClickListener ic) {
this.itemclicklistener = ic;
}
#Override
public void onClick(View view) {
this.itemclicklistener.onItemClick(view,getAdapterPosition());
}
}
}
so and ideas?!

Categories