根据SQLite表列值将setVisiblitiy设置为ImageView

时间:2015-05-17 19:56:03

标签: android sqlite android-recyclerview android-viewholder

我在Material Design中做了一个应用程序,我的目标是从SQLite数据库获取项目,如果项目行boolean is_checked为1,则在RecyclerView中显示带有ImageVIew的项目。我知道这个解释有点不对,所以这是我的代码:

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

我与数据库沟通的主要活动:

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

我的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);




        }

如您所见,我尝试使用以下代码检查此字段:

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

但它不起作用。

我已经检查了我的数据库并检查并取消选中方法工作,即数据库列值正确更改。

0 个答案:

没有答案