简体   繁体   English

如何将 OnClickListener 设置为 ListView 内的按钮,该按钮也具有项目的 id

[英]How to setOnClickListener to a button inside a ListView that also have the id of the item

I am creating an app that tracks the inventory for a shop.我正在创建一个跟踪商店库存的应用程序。 The layout consists of a ListView that has a sale button for each item, and on clicking the sale button, the quantity of the item should be decreased by one and be updated in database.布局由一个 ListView 组成,每个项目都有一个销售按钮,点击销售按钮后,该项目的数量应该减少一个并在数据库中更新。 So my question is how do i set the onClickListener to the button so that it also receives the id of the item for which the sale button was clicked for, as it will enable me to update only the specific item in the database.所以我的问题是如何将 onClickListener 设置为按钮,以便它也接收点击销售按钮的项目的 id,因为这将使我能够仅更新数据库中的特定项目。

This is an sample example这是一个示例

The Button in the fxml file must have an fx:id so that you can use the button properties in the code behind fxml 文件中的 Button 必须有一个 fx:id 以便您可以在后面的代码中使用按钮属性

it looks like this它看起来像这样

<Button fx:id="myButton"/>

and then you can use it (eg in your controller) like that然后你可以像这样使用它(例如在你的控制器中)

myButton.setOnClickListener(new OnClickListener(){
         public void onClick(View v) {
         // TODO
         }
});

please check this also setOnClickListener请检查这也setOnClickListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM