gpt4 book ai didi

java - 在适配器中保存复选框位置/值

转载 作者:太空宇宙 更新时间:2023-11-04 10:22:53 25 4
gpt4 key购买 nike

我有一个自定义 ListView,其中包含 TextView 和复选框。用户一次只能选择一个复选框。当用户按下后退按钮时,我能够使用 SharedPrefences 保留它,但是问题是,一旦用户返回主 Activity,我无法弄清楚如何检索在适配器中选择了哪个复选框。

我觉得一切都应该在适配器中完成,以检索它,对吧?

我不确定我的解释是否正确,请告诉我您是否需要更多信息。

package company.tuyu.tuyu;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;

public class ListAdapterUserBooking extends ArrayAdapter<Bookings> {

private Bookings bookings;

private TextView user_book_total;

private static final String PREFERENCES_NAMESPACE = "checkboxes_states";

int selected_position = -1;

public ListAdapterUserBooking(Context context, ArrayList<Bookings> bookings) {
super(context, R.layout.custom_user_bookinglistview, bookings);
}

@Override
public int getCount() {
return super.getCount();
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
bookings = getItem(position);

LayoutInflater inf = LayoutInflater.from((getContext()));

View customView = inf.inflate(R.layout.custom_user_bookinglistview, parent, false);

final TextView serviceName = (TextView) customView.findViewById(R.id.custom_user_booking_serviceName);
TextView servicePrice = (TextView) customView.findViewById(R.id.custom_user_booking_servicePrice);
final CheckBox checkBox = (CheckBox) customView.findViewById(R.id.custom_user_booking_checkBox);

user_book_total = (TextView) ((Activity) getContext()).findViewById(R.id.user_book_total);

serviceName.setText(bookings.serviceName[position]);
servicePrice.setText("£" + bookings.servicePrice[position]);

checkBox.setChecked(position == selected_position);

final SharedPreferences sharedPreferences = getContext().getSharedPreferences(PREFERENCES_NAMESPACE, Context.MODE_PRIVATE);

checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bookings.totalPrice = bookings.servicePrice[position];

final boolean isChecked = checkBox.isChecked();
if (isChecked) {
selected_position = position;

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("checkBoxChecked", true);
editor.apply();

bookings.selectedServiceID = bookings.ID[position];
bookings.selectedServicePrice = bookings.servicePrice[position];

user_book_total.setText("£" + bookings.totalPrice);
} else {
selected_position = -1;

user_book_total.setText("£" + "0" + ".00");
}

notifyDataSetChanged();
}
});

return customView;
}
}

最佳答案

我自己修复了这个问题,但忘记发布修复内容,这就是我所做的:

package company.tuyu.tuyu;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;

public class ListAdapterUserBooking extends ArrayAdapter<Bookings> {

private Bookings bookings;

private TextView user_book_total;

int selected_position = -1;

public ListAdapterUserBooking(Context context, ArrayList<Bookings> bookings) {
super(context, R.layout.custom_user_bookinglistview, bookings);
}

@Override
public int getCount() {
return super.getCount();
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
bookings = getItem(position);

LayoutInflater inf = LayoutInflater.from((getContext()));

View customView = inf.inflate(R.layout.custom_user_bookinglistview, parent, false);

final TextView serviceName = (TextView) customView.findViewById(R.id.custom_user_booking_serviceName);
TextView servicePrice = (TextView) customView.findViewById(R.id.custom_user_booking_servicePrice);
final CheckBox checkBox = (CheckBox) customView.findViewById(R.id.custom_user_booking_checkBox);

user_book_total = (TextView) ((Activity) getContext()).findViewById(R.id.user_book_total);

serviceName.setText(bookings.serviceName[position]);
servicePrice.setText("£" + bookings.servicePrice[position]);

final SharedPreferences sharedPreferences = getContext().getSharedPreferences("sharedPref", Context.MODE_PRIVATE);

selected_position = sharedPreferences.getInt("selectedPosition", 0);
checkBox.setChecked(selected_position == position);

bookings.totalPrice = bookings.servicePrice[selected_position];
bookings.selectedServiceID = bookings.ID[selected_position];
bookings.selectedServicePrice = bookings.servicePrice[selected_position];

user_book_total.setText("£" + bookings.totalPrice);

checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bookings.totalPrice = bookings.servicePrice[position];

final boolean isChecked = checkBox.isChecked();
if (isChecked) {
selected_position = position;

bookings.selectedServiceID = bookings.ID[position];
bookings.selectedServicePrice = bookings.servicePrice[position];

user_book_total.setText("£" + bookings.totalPrice);
} else {
selected_position = -1;

user_book_total.setText("£" + "0" + ".00");
}

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("selectedPosition", selected_position);
editor.apply();

notifyDataSetChanged();
}
});

return customView;
}
}

关于java - 在适配器中保存复选框位置/值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50898886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com