简体   繁体   English

从回收站视图按钮单击更改不同的按钮文本

[英]Change Different Button Text from Recycler View Button Click

In my main activity page, i have a recycler view that has buttons for each item.在我的主活动页面中,我有一个回收器视图,每个项目都有按钮。 As i click an item button, i want it to update the text on the Shopping cart button to show the count of items in the cart.The shopping cart button is outside of the recycler view.当我单击一个项目按钮时,我希望它更新购物车按钮上的文本以显示购物车中的项目计数。购物车按钮位于回收站视图之外。

I already setup a global variable that records the count of items in the cart, but i can't figure out how to display that count on the button.我已经设置了一个记录购物车中商品数量的全局变量,但我不知道如何在按钮上显示该数量。 Mostly, i can't figure out how that button text can update when i click another item in the recycler view.大多数情况下,当我单击回收站视图中的另一个项目时,我无法弄清楚该按钮文本如何更新。

Thanks in advance!提前致谢!

In my Main Activity (where the recycler view is located and the button i want the text to change on is located), I've created a method init2.在我的主活动(回收器视图所在的位置以及我希望文本更改的按钮所在的位置)中,我创建了一个方法 init2。

public void init2() {
    shopbutton = (Button) findViewById(R.id.button2);
   String countshopS = "hello";
   shopbutton.setText(countshopS);
}

then i try to call the method init2 from the Onclick Listener of the Adapter where the button rows are.然后我尝试从按钮行所在的适配器的 Onclick 侦听器调用方法 init2。

public class MyAdapter  extends RecyclerView.Adapter<MyAdapter.ViewHolder> 
{

private MainActivity main = new MainActivity();


holder.priceset.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onClick(View v) {

            main.init2();

I get the response "Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference" on the row where the Id of the button is identified in init2();我在 init2() 中标识按钮的 Id 的行上收到响应“Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference”;

Use callback in your Adapter and increase for every click on row's button .在您的适配器中使用回调并在每次单击行按钮时增加。

Then implement callback in your activity and do all things you need for example show finial price or count or...然后在您的活动中实现回调并执行您需要的所有操作,例如显示最终价格或计数或...

Do not use statics and handle every rows count with data model (transiant maybe useful) and callbacks and interfaces.不要使用静态并使用数据模型(瞬态可能有用)和回调和接口处理每一行计数。

暂无
暂无

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

相关问题 单击按钮更改回收站视图中的数据 - change data in recycler view with click of button 单击按钮后,在回收站视图中显示AlertDialog - AlertDialog in recycler view on button click 单击按钮从回收站视图中删除项目。按钮是从回收站视图外部设置的? - Delete Item From Recycler View on Button Click.Button is Set from outside of Recycler View? 从回收站项目按钮单击更改主布局 - Change main layout from Recycler item button click 单击按钮时如何在回收器视图中更新textview? - How to update textview in recycler view on button click? 每个按钮单击可更改文本视图 - every button click to change text view 如何在每个回收站视图项目内单击按钮时实现文本计数器 - How to implement text counter on button click inside each recycler view item 使用 espresso 在回收站视图中单击孩子的按钮 - Performing click on a button of a child in recycler view using espresso 我有一个 Recycler View,onRun 会显示,但是单击按钮后它将是空的,我想设置一个默认的 recycler 视图 - I have a Recycler View, onRun will display, but upon a button click it will be empty, I want to set a default recycler view 我在回收站视图中显示多个编辑文本。 如何在按钮单击侦听器上检查编辑文本的第一个到最后所有 position 是否为空 - I am showing Multiple Edit text in Recycler view . How to check the first to last all position of Edit text is empty or not on button click listener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM