Im doing an app in Material Design and my goal is to get items form SQLite database and if an item row boolean is_checked is 1, then display this item with an ImageVIew in RecyclerView. I know this explanation is bit off so here is my code:
DbHandler:
public void deleteProduct(String productname){
SQLiteDatabase db=getWritableDatabase();
String[] args = {productname};
db.delete(TABLE_PRODUCTS, COLUMN_PRODUCTNAME + "= ?", args);
}
public void changeProduct(Product product){
ContentValues values= new ContentValues();
values.put(COLUMN_PRODUCTNAME,product.get_productname());
values.put(COLUMN_ISCHECKED,product.is_checked());
SQLiteDatabase db=getWritableDatabase();
db.insert(TABLE_PRODUCTS,null,values);
db.close();
}
My main activity where I communicate with database:
RecyclerView recyclerView;
CustomAdapter adapter;
ArrayList<Product> productnames=new ArrayList<>();
DBHandler dbhandler;
Toolbar toolbar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
recyclerView=(RecyclerView)findViewById(R.id.productList);
FloatingActionButton fab= (FloatingActionButton) findViewById(R.id.fab);
fab.attachToRecyclerView(recyclerView);
dbhandler=new DBHandler(this,null,null,1);
adapter= new CustomAdapter(this,productnames,this,this);
getProductsFromDb();
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
And my CustomAdapter :
private LayoutInflater inflater;
List<Product> products= Collections.emptyList();
public DbItemDeleteListener deleteListener;
public DbItemChangeListener changeListener;
public CustomAdapter(Context context,List<Product> products,DbItemDeleteListener deleteListener,DbItemChangeListener changeListener){
inflater=LayoutInflater.from(context);
this.products=products;
this.deleteListener = deleteListener;
this.changeListener = changeListener;
}
public void check(int position){
Product product=products.get(position);
product.set_checked(true);
}
public void uncheck(int position){
Product product=products.get(position);
product.set_checked(false);
}
public Product getProduct(int position){
Product product=products.get(position);
return product;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.custom_list_row,parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Product currentProduct=products.get(position);
holder.productname.setText(currentProduct.get_productname());
if(currentProduct.is_checked()) {
holder.checkIcon.setVisibility(View.VISIBLE);
}
else {
holder.checkIcon.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return products.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView productname;
Button checkButton;
Button deleteButton;
ImageView checkIcon;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
productname= (TextView) itemView.findViewById(R.id.ProductName);
checkButton= (Button) itemView.findViewById(R.id.checkButton);
deleteButton= (Button) itemView.findViewById(R.id.deleteButton);
checkIcon= (ImageView) itemView.findViewById(R.id.checkIcon);
}
As you can see I tried to check this field with the code:
public void onBindViewHolder(MyViewHolder holder, int position) {
Product currentProduct=products.get(position);
holder.productname.setText(currentProduct.get_productname());
if(currentProduct.is_checked()) {
holder.checkIcon.setVisibility(View.VISIBLE);
}
else {
holder.checkIcon.setVisibility(View.GONE);
}
}
But it wont work.
I have checked my database and check and uncheck methods works aka the database column value changes correctly.