嘿我正在尝试使用ArrayList
过滤我的SwitchCompat
,并在RecyclerView
中显示已过滤的列表,我正在使用此代码:
cancelled = (SwitchCompat) inc.findViewById(R.id.cancelledSwitch);
cancelledByAdmin = (SwitchCompat) inc.findViewById(R.id.cancelledByAdminSwitch);
reserved = (SwitchCompat) inc.findViewById(R.id.reservedSwitch);
pending = (SwitchCompat) inc.findViewById(R.id.pendingSwitch);
deprecated = (SwitchCompat) inc.findViewById(R.id.deprecatedSwitch);
deprecated.setOnCheckedChangeListener(filterswitch);
cancelled.setOnCheckedChangeListener(filterswitch);
pending.setOnCheckedChangeListener(filterswitch);
cancelledByAdmin.setOnCheckedChangeListener(filterswitch);
reserved.setOnCheckedChangeListener(filterswitch);
CompoundButton.OnCheckedChangeListener filterswitch = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
filter();
}
};
public void filter() {
ArrayList<ReservedTimesModel> filtered = new ArrayList<>();
for (ReservedTimesModel reservedTimesModel : arrayList) {
switch (reservedTimesModel.res_status) {
case "CanceledByAdmin":
if (cancelledByAdmin.isChecked()) {
filtered.add(reservedTimesModel);
}
break;
case "Canceled":
if (cancelled.isChecked()) {
filtered.add(reservedTimesModel);
}
break;
case "Pending":
if (pending.isChecked()) {
filtered.add(reservedTimesModel);
}
break;
case "Deprecated":
if (pending.isChecked()) {
filtered.add(reservedTimesModel);
}
break;
default:
if (reserved.isChecked()) {
filtered.add(reservedTimesModel);
}
}
}
recyclerView.setAdapter(new ReservedTimesAdapter(filtered, ReservedTimesActivity.this));
}
ReservedTimesAdapter:
public class ReservedTimesAdapter extends RecyclerView.Adapter<ReservedTimesAdapter.ViewHolder> {
ArrayList<ReservedTimesModel> arrayList;
Context context;
StyleSpan bold = new StyleSpan(android.graphics.Typeface.BOLD);
public ReservedTimesAdapter(ArrayList<ReservedTimesModel> arrayList, Activity context) {
this.arrayList = arrayList;
this.context = context;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
AppCompatButton cancel;
TextView node_titletxt, timetxt, datetxt, statustxt, doctxt;
LinearLayout linearLayout;
public ViewHolder(View v) {
super(v);
cancel = (AppCompatButton) v.findViewById(R.id.cancel_reserve);
statustxt = (TextView) v.findViewById(R.id.reserved_status);
datetxt = (TextView) v.findViewById(R.id.reserved_date);
timetxt = (TextView) v.findViewById(R.id.reserved_time);
node_titletxt = (TextView) v.findViewById(R.id.node_title);
doctxt = (TextView) v.findViewById(R.id.reserved_doctor);
linearLayout = (LinearLayout) v.findViewById(R.id.linearlayout);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.reserved_times_list_layout, parent, false));
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
if (arrayList.get(position).cancelable) {
if (arrayList.get(position).res_status.equals("Reserved")) {
holder.cancel.setVisibility(View.VISIBLE);
holder.cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click(position);
}
});
} else {
holder.cancel.setVisibility(View.GONE);
}
}else{
holder.cancel.setVisibility(View.GONE);
}
SpannableStringBuilder date = new SpannableStringBuilder("تاریخ: " + convert_num(arrayList.get(position).year + "/" + arrayList.get(position).month + "/" + arrayList.get(position).day));
date.setSpan(bold,0,6, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
holder.datetxt.setText(date);
String title;
if (arrayList.get(position).typename.equals("Hospital")) {
title = "بیمارستان";
} else {
title = "کلینیک";
}
SpannableStringBuilder doc = new SpannableStringBuilder("دکتر: " + arrayList.get(position).doc);
doc.setSpan(bold, 0, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
holder.doctxt.setText(doc);
SpannableStringBuilder node_title = new SpannableStringBuilder(title + " " + arrayList.get(position).node_title);
node_title.setSpan(bold, 0, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
holder.node_titletxt.setText(node_title);
SpannableStringBuilder time = new SpannableStringBuilder("زمان: " + convert_num(arrayList.get(position).ft_to_time.substring(0, 5) + " - " + arrayList.get(position).ft_of_time.substring(0, 5)));
time.setSpan(bold, 0, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
holder.timetxt.setText(time);
String status;
Drawable drawable = context.getResources().getDrawable(R.drawable.textview_shape);
switch (arrayList.get(position).res_status) {
case "CanceledByAdmin":
status = context.getString(R.string.cancelled_by_admin);
drawable.setColorFilter(0xFFFC635D, PorterDuff.Mode.SRC);
break;
case "Canceled":
status = context.getString(R.string.cancelled);
drawable.setColorFilter(0xFFEE903D, PorterDuff.Mode.SRC);
break;
case "Pending":
status = context.getString(R.string.pending);
drawable.setColorFilter(0xFFd25913, PorterDuff.Mode.SRC);
break;
case "Deprecated":
status = context.getString(R.string.deprecated);
drawable.setColorFilter(0xFF757575, PorterDuff.Mode.SRC);
break;
default:
status = context.getString(R.string.reserved);
drawable.setColorFilter(0xFF3BA757, PorterDuff.Mode.SRC);
}
holder.statustxt.setBackground(drawable);
holder.statustxt.setText(status);
}
public String convert_num(String string) {
string = string.replace("0", "۰").replace("1", "۱").replace("2", "۲").replace("3", "۳").replace("4", "۴").replace("5", "۵").replace("6", "۶").replace("7", "۷").replace("8", "۸").replace("9", "۹");
return string;
}
@Override
public int getItemCount() {
return arrayList.size();
}
public void click(final int position) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(R.layout.dialog_layout);
alertDialogBuilder.setPositiveButton("بله", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Trans trans = new Trans();
trans.getPaymentMode(context, new AsyncResponse<String>() {
@Override
public void onResponse(String response) {
if (response.replace("\"", "").equals("Direct")) {
trans.cancelReserve(context, new AsyncResponse<String>() {
@Override
public void onResponse(String response) {
if (response.equals("true")) {
AlertDialog.Builder alertDialogBuilder1 = new AlertDialog.Builder(context);
alertDialogBuilder1.setView(R.layout.dialog_layout);
alertDialogBuilder1.setPositiveButton("ادامه", null);
AlertDialog alertDialog1 = alertDialogBuilder1.create();
alertDialog1.show();
alertDialog1.getButton(DialogInterface.BUTTON_POSITIVE).setTextSize(18);
TextView title = (TextView) alertDialog1.findViewById(R.id.title);
title.setText("توجه!");
TextView tet = (TextView) alertDialog1.findViewById(R.id.text);
tet.setText("رزرو با موفقیت لغو شد.");
arrayList.get(position).res_status = "Canceled";
synchronized (this) {
for (int i = 0; i < arrayList.size(); i++) {
notifyItemChanged(i);
}
}
} else {
AlertDialog.Builder alertDialogBuilder1 = new AlertDialog.Builder(context);
alertDialogBuilder1.setView(R.layout.dialog_layout);
alertDialogBuilder1.setPositiveButton("ادامه", null);
AlertDialog alertDialog1 = alertDialogBuilder1.create();
alertDialog1.show();
alertDialog1.getButton(DialogInterface.BUTTON_POSITIVE).setTextSize(18);
TextView title = (TextView) alertDialog1.findViewById(R.id.title);
title.setText("خطا!");
TextView tet = (TextView) alertDialog1.findViewById(R.id.text);
tet.setText("عملیات لغو رزرو وقت موفقیتآمیز نبود. لطفا مجدداً تکرار فرمایید.");
}
}
@Override
public void onErrorResponse(String error) {
AlertDialog.Builder alertDialogBuilder1 = new AlertDialog.Builder(context);
alertDialogBuilder1.setView(R.layout.dialog_layout);
alertDialogBuilder1.setPositiveButton("ادامه", null);
AlertDialog alertDialog1 = alertDialogBuilder1.create();
alertDialog1.show();
alertDialog1.getButton(DialogInterface.BUTTON_POSITIVE).setTextSize(18);
TextView title = (TextView) alertDialog1.findViewById(R.id.title);
title.setText("خطا!");
TextView tet = (TextView) alertDialog1.findViewById(R.id.text);
tet.setText("عملیات لغو رزرو وقت موفقیتآمیز نبود. لطفا مجدداً تکرار فرمایید.");
}
}, arrayList.get(position).ft_id, arrayList.get(position).res_id);
} else {
Intent intent = new Intent(context, CreditCardActivity.class);
intent.putExtra("ft_id", arrayList.get(position).ft_id);
intent.putExtra("res_id", arrayList.get(position).res_id);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
@Override
public void onErrorResponse(String error) {
}
}, arrayList.get(position).node_id);
}
});
alertDialogBuilder.setNegativeButton("خیر", null);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextSize(18);
alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextSize(18);
TextView title = (TextView) alertDialog.findViewById(R.id.title);
title.setText("تایید لغو رزرو");
TextView tet = (TextView) alertDialog.findViewById(R.id.text);
tet.setVisibility(View.GONE);
}
我arrayList
的大小是18,但是当我点击其中一个SwitchCompat
时,它会滞后大约1秒。为什么会这样?
答案 0 :(得分:2)
setAdapter
。
您创建一个适配器(在您的活动中onCreate()
),每次数据更改时再次将数据设置为适配器并调用myAdapter.notifyDataSetChanged();
将此方法添加到ReservedTimesAdapter
类
public void setList( ArrayList<ReservedTimesModel> arrayList)
{
this.arrayList = arrayList;
}
然后定义一个ReservedTimesAdapter
对象并将setAdapter
代码添加到您的活动onCreate()
类中。就像这样:
ReservedTimesAdapter myAdapter= new ReservedTimesAdapter(filtered, ReservedTimesActivity.this);
recyclerView.setAdapter(myAdapter);
并更改filter
方法,如下所示:
public void filter() {
ArrayList<ReservedTimesModel> filtered = new ArrayList<>();
for (ReservedTimesModel reservedTimesModel : arrayList) {
switch (reservedTimesModel.res_status) {
case "CanceledByAdmin":
if (cancelledByAdmin.isChecked()) {
filtered.add(reservedTimesModel);
}
break;
case "Canceled":
if (cancelled.isChecked()) {
filtered.add(reservedTimesModel);
}
break;
case "Pending":
if (pending.isChecked()) {
filtered.add(reservedTimesModel);
}
break;
case "Deprecated":
if (pending.isChecked()) {
filtered.add(reservedTimesModel);
}
break;
default:
if (reserved.isChecked()) {
filtered.add(reservedTimesModel);
}
}
}
myAdapter.setList(filtered);
myAdapter.notifyDataSetChanged();
}