我可以将方法中的变量传递给适配器中的文本视图吗?

Can I pass a variable in a method to a textview in an adapter?

我想通过

int count = cursor.getCount();

来自

public List<Products> getAllProducts() {
        String[] columns = {
                COLUMN_ID,
                COLUMN_BARCODE,
                COLUMN_NAME,
                COLUMN_OPRICE,
                COLUMN_SPRICE,
                COLUMN_XPDATE
        };

        String sortOrder =
                COLUMN_ID + " DESC";
        List<Products> productList = new ArrayList<Products>();
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_PRODUCT,
                columns,
                null,
                null,
                COLUMN_NAME,
                null,
                sortOrder);
        if (cursor.moveToFirst()) {
            do {
                Products product = new Products();
                product.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_ID))));
                product.setBarcode(cursor.getString(cursor.getColumnIndex(COLUMN_BARCODE)));
                product.setName(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
                product.setOprice(cursor.getString(cursor.getColumnIndex(COLUMN_OPRICE)));
                product.setSprice(cursor.getString(cursor.getColumnIndex(COLUMN_SPRICE)));
                product.setDate(cursor.getString(cursor.getColumnIndex(COLUMN_XPDATE)));
                productList.add(product);
            } while (cursor.moveToNext());
        }
        **int count = cursor.getCount();**
        cursor.close();
        db.close();
        return productList;
    }

到适配器中的文本视图

holder.total.setText("counts");

适配器

public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ProductViewHolder>{
    private List<Products> listProducts;
    DatabaseHelper databaseHelper;
    public ProductsAdapter(List<Products> listProducts) {
        this.listProducts= listProducts;
    }
    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.products_list, parent, false);
        return new ProductViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        holder.name.setText(listProducts.get(position).getName());
        holder.total.setText("counts");
    }

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

    public class ProductViewHolder extends RecyclerView.ViewHolder {
        private TextView name, total;
        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.productName);
            total = itemView.findViewById(R.id.available);
        }
    }
}

我建议您将要使用的变量设为全局变量(例如在方法外定义它)然后在方法内实例化它然后调用具有您要传递的变量的 class

例如

Myclass class = new Myclass()
Int count = class.count

然后将 Int 转换为字符串,因为文本视图只接受 String 例如String countSt = String.ValueOf(count) 谢谢

这一行:

int count = cursor.getCount();

游标中包含的行数赋值给变量count
这些行都添加到列表 productList.
所以,实际上你不需要变量 count 因为你可以通过获取列表的大小来获得相同的数字。

此列表在您的适配器 class 中作为 listProducts 传递,因此获取其大小并将其显示在 TextView:

holder.total.setText(String.valueOf(listProducts.size()));