在适配器

时间:2018-06-17 17:22:55

标签: java android

我有一个包含textview和复选框的自定义ListView。用户当时只能选择一个复选框。当用户按下按钮时,我能够使用SharedPrefrences保持它,但问题是,一旦用户返回主Activity,我无法弄清楚如何检索在Adapter中选择了哪个复选框。

我觉得应该在Adapter中完成一切,检索它,对吗?

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

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;
    }
}

1 个答案:

答案 0 :(得分:0)

我自己修复了此问题,但忘了发布此修复程序,这是我所做的:

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;
    }
}