如何从视图持有者的sqlite中获取值

问题描述 投票:0回答:1

产品适配器

        public class ProductAdapter extends RecyclerView.Adapter<ProductViewHolder>{

            private Context context;
            private List<Product> listProducts;

            private SqliteDatabase mDatabase;

            public ProductAdapter(Context context, List<Product> listProducts) {
                this.context = context;
                this.listProducts = listProducts;
                mDatabase = new SqliteDatabase(context);
            }

            @Override
            public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_list_layout, parent, false);
                return new ProductViewHolder(view);
            }

            @Override
            public void onBindViewHolder(ProductViewHolder holder, int position) {
                final Product singleProduct = listProducts.get(position);
                  holder.name.setText(singleProduct.getName());

               // holder.quantity.setText(singleProduct.getQuantity());
               //when i call quantity(the ABOVE value) the application crashes and when i call only name the field the values gets displayed 

                holder.editProduct.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        editTaskDialog(singleProduct);
                    }
                });

                holder.deleteProduct.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                       mDatabase.deleteProduct(singleProduct.getId());
                       ((Activity)context).finish();
                        context.startActivity(((Activity) context).getIntent());
                    }
                });
            }

            @Override
            public int getItemCount() {
                return listProducts.size();
            }

            private void editTaskDialog(final Product product){
                LayoutInflater inflater = LayoutInflater.from(context);
                View subView = inflater.inflate(R.layout.add_product_layout, null);
                final EditText nameField = (EditText)subView.findViewById(R.id.enter_name);
                final EditText quantityField = (EditText)subView.findViewById(R.id.enter_quantity);

                if(product != null){
                    nameField.setText(product.getName());
                    quantityField.setText(String.valueOf(product.getQuantity()));
                }

                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Edit product");
                builder.setView(subView);
                builder.create();
                builder.setPositiveButton("EDIT PRODUCT", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        final String name = nameField.getText().toString();
                        final String quantityStr = quantityField.getText().toString();

                        if(TextUtils.isEmpty(name) || TextUtils.isEmpty(quantityStr))
                        {
                            Toast.makeText(context, "Something went wrong. Check your input values", Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                            final int quantity = Integer.parseInt(quantityStr);

                            if(quantity<=0)
                            {
                                Toast.makeText(context,"input is less than or equal to zero",Toast.LENGTH_LONG).show();
                            }
                            else {

                                mDatabase.updateProduct(new Product(product.getId(), name, quantity));
                                //refresh the activity
                                ((Activity)context).finish();
                                context.startActivity(((Activity)context).getIntent());
                            }
                       }
                     }
                });

        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "Task cancelled", Toast.LENGTH_LONG).show();
                    }
                });
                builder.show();
            }
        }

产品查看。持有人

        public class ProductViewHolder extends RecyclerView.ViewHolder {
            public TextView name;
            public TextView quantity;
            public ImageView deleteProduct;
            public  ImageView editProduct;

            public ProductViewHolder(View itemView) {
                super(itemView);
                name = (TextView)itemView.findViewById(R.id.product_name);
                deleteProduct = (ImageView)itemView.findViewById(R.id.delete_product);
                editProduct = (ImageView)itemView.findViewById(R.id.edit_product);
                quantity=(TextView)itemView.findViewById(R.id.product_name2);
            }
        }

现在这里的问题是什么时候使用holder.quantity.setText(singleProduct.getQuantity())应用程序崩溃。

我还想添加日期选择器对话框,并希望在回收器视图中显示它,所以对于日期代码是相同的,如holder.object.setText或其他东西,我们可以使用表单作为警报对话框,如

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
dialog.setContentView(R.layout.dialog_dark);
dialog.setCancelable(true);

我创建了数据库文件,但问题主要与回收站视图内容有关,如果我想要多个持有者,是否可以获取内容?

java android
1个回答
0
投票

getQuantity()返回int。因此,Android正在尝试使用id == getQuantity()搜索资源导致崩溃(因为你可能有任何资源与id

换句话说,你正在调用TextView.setText(int resourceId)而不是TextView.setText(String text))。

要修复,请更改:

holder.quantity.setText(singleProduct.getQuantity());

对此:

holder.quantity.setText(String.valueOf(singleProduct.getQuantity()));
© www.soinside.com 2019 - 2024. All rights reserved.