【发布时间】:2018-11-03 10:22:25
【问题描述】:
我在 ItemActivity 中有一个 Recycler View 及其适配器作为单独的类 ItemAdapter。现在根据所选项目的计数,我需要更新放置在 ItemActivity 中的按钮上的文本。如何完成?
注意:我已将 getApplicationContext() 作为上下文传递给适配器。它传递了我在清单中设置的 GlobalValue 上下文。
<application
android:name=".GlobalValues"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">
以下是我的 ItemActivity.xaml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
tools:context=".ItemPickerActivity">
<TextView
android:id="@+id/itemstext"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Items"
android:fontFamily="@font/exo_semibold"
android:textColor="@color/dark_grey"
/>
<android.support.v7.widget.RecyclerView
android:layout_marginHorizontal="3dp"
android:id="@+id/pickerRecview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/itemstext"
android:layout_marginTop="7dp" />
<RelativeLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="10dp"
android:id="@+id/checkout"
android:clickable="true"
android:background="@color/go_green"
>
<TextView
android:id="@+id/Total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs XXX"
android:layout_marginLeft="5dp"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_centerVertical="true"
android:fontFamily="@font/exo_semibold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkout"
android:textColor="@color/white"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="22sp"
android:fontFamily="@font/exo_semibold"
/>
<TextView
android:id="@+id/TotalItems"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="xx Items"
android:fontFamily="@font/exo_semibold"
android:textColor="@color/white"
android:textSize="18sp" />
</RelativeLayout>
</RelativeLayout>
这就是我实例化适配器对象的方式
ItemPickerAdapter adapter = new ItemPickerAdapter(getApplicationContext(), ItemsList);
recyclerView.setAdapter(adapter);
我需要从适配器更改我的 ItemActitvity 中 ItemCount 的值!请告诉我如何可能。谢谢!
【问题讨论】:
-
使用带有回调的接口。
-
您可以将监听器从您的活动传递给适配器,查看此示例>> stackoverflow.com/a/49397985/8009433
标签: java android android-recyclerview