2016-07-16 6 views
0

用のアダプタを設定するときにちょっと私はRecyclerViewでリストをフィルタリングSwitchCompat、ショーで私のArrayListをフィルタ処理しようとしていると私はこのコードを使用しています:ラグのArrayListをフィルタリングし、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

あなたのアプリをプロファイルしようとしましたか? [this](https://developer.android.com/studio/profile/traceview.html) – pskink

+0

を参照してください。ボトルネックは 'onBindViewHolder'の中に' 'Html.fromHtml''があり、代わりに' Spannable#setSpan'を使用しました。あなたのアプリをプロファイルしますか?上に投稿したリンクを見たことがありますか? – pskink

+0

ええ、それを確認しましたが、@ pskink –

答えて

2

データが変更されるたびにsetAdapterする必要はありません。 あなたがして(あなたの活動onCreate()に)一度あなたのデータの変更は、データが再びアダプタと呼び出すように設定するたびにアダプタを作成myAdapter.notifyDataSetChanged();

public void setList(ArrayList<ReservedTimesModel> arrayList) 
    { 
     this.arrayList = arrayList; 
    } 

が、その後ReservedTimesAdapterオブジェクトを定義し、追加ReservedTimesAdapterクラスにこのメソッドを追加このclass.likeあなたの活動onCreate()からsetAdapterコード:

ReservedTimesAdapter myAdapter= new ReservedTimesAdapter(filtered, ReservedTimesActivity.this); 

recyclerView.setAdapter(myAdapter); 

とを変更の方法は次のとおりです。

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(); 
} 
+1

はい、私は' new ReservedTimesAdapter() 'と' recyclerView.setAdapter() 'の両方が時間のかかる作業であると信じています –

+0

投稿コードどのようにデータを設定できますか?私が試したすべてが@AlexCohnを働かせなかったので私のアダプター –

+1

それは遅れを減らしてくれてありがとう –

関連する問題