选中复选框时,将布尔值设置为Realm数据库

时间:2016-09-07 14:27:54

标签: java android-studio realm

我有一个我希望在arecyclerview中显示的领域数据库模型, 当我尝试在RecyclerviewAdapter中保存布尔值时,数据库模型中的每一行都有一个String名称,int age和一个boolean favorite 通过使用界面,但我{g} java.lang.NullPointerException: Attempt to invoke virtual method 'void io.realm.Realm.beginTransaction()' on a null object reference

这个模型

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;
}

}

这是界面

  public interface ItemClickListener {
void onItemClick(View view,int position);}

这就是recyclerview适配器

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());

    }
}

}

所以和想法?!

0 个答案:

没有答案