简体   繁体   English

如何将值从适配器传递到另一个 class

[英]How to pass value from adapter to another class

I am trying to get switch state from item view我正在尝试从项目视图中获取开关 state

this is the layout file which holds the switch and i wanted to get if switch is checked from another class这是包含开关的布局文件,我想知道是否从另一个 class 检查开关

i want to either do it in the adapter and to pass a value from the adapter to another class by checking if the switch is checked or get the layout from class and check if the switch is checked我想在适配器中执行此操作并将值从适配器传递到另一个 class 通过检查是否检查开关或从 class 获取布局并检查是否检查开关

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <TextView
        android:id="@+id/studentName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Student Name"
        android:textSize="25dp"/>

    <Switch
        android:id="@+id/attendanceSwitch"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="25dp"
        android:checked="true"/>

</RelativeLayout>

and this is my adapter这是我的适配器

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

    private Context context;
    private List<User> users;
    public StudentAdapter(Context context, List<User> users) {
        this.context = context;
        this.users = users;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.student_item, parent, false);
        return new StudentAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final User user = users.get(position);


        holder.studentName.setText(user.getStudentname() + " " + user.getLastname());

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView studentName;
        public Switch aSwitch;

        public ViewHolder(View itemView) {
            super(itemView);

            studentName = itemView.findViewById(R.id.studentName);
            aSwitch = itemView.findViewById(R.id.attendanceSwitch);
        }
    }
}

use SharedPreferences使用 SharedPreferences

public SharedPreferences sharedpreferences = getSharedPreferences("mySharedPreferences", MODE_PRIVATE);
public SharedPreferences.Editor PrefsEditor = sharedpreferences.edit();

store data存储数据

PrefsEditor.putString("value", "MyString").apply();

get data获取数据

String value = sharedpreferences.getString("value", "");

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

相关问题 如何将 textview 值从适配器 class 传递到另一个活动的 texview - How to pass textview value from adapter class to texview of another acivity 如何将值从一个活动传递到另一个活动/适配器 - How to Pass a Value from One Activity to Another Activity/Adapter 如何从适配器recycleview传递/获取值复选框到另一个活动 - How to pass/get value checkbox from adapter recycleview to another activity 如何将值从按钮传递到另一个类 - How to pass a value from a button to another class 如何获取一个类的值并将其传递给另一个类? - How to get and pass a value from a class to another? 目的问​​题:如何使用另一个“适配器”类中的MainActivity将数据传递给SecondActivity - Intent problem: how to use MainActivity from another class “Adapter” to pass data to SecondActivity 如何将值从一个类传递到另一类GUI - How to pass value from one class to another class GUI 如何将 JSpinner 值从 Java class 传递给另一个 Java class - How to pass JSpinner value from Java class to another Java class 如何将值从一个 class 的 onOptionsItemSelected 方法传递到另一个 class - How to pass value from onOptionsItemSelected method of one class to another class 如何将变量值从主 class 传递给另一个 class? - How to pass the value of variables from main class to another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM