Ansaid

Секретный адаптер

Feb 20th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.22 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8. import androidx.recyclerview.widget.RecyclerView;
  9.  
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
  14.  
  15.     private int size;
  16.     private Context context;
  17.  
  18.     private final static String[][] num = new String[][] {
  19.             {"", "один", "два", "три", "четыре", "пять", "шесть", "семь", "восемь", "девять"},
  20.             {"", "десять ", "двадцать ", "тридцать ", "сорок ", "пятьдесят ", "шестьдесят ", "семьдесят ", "восемьдесят ", "девяносто "},
  21.             {"", "сто ", "двести ", "триста ", "четыреста ", "пятьсот ", "шестьсот ", "семьсот ", "восемьсот ", "девятьсот "},
  22.             {"тысяч ", "одна тысяча ", "две тысячи ", "три тысячи ", "четыре тысячи ", "пять тысяч ", "шесть тысяч ", "семь тысяч ", "восемь тысяч ", "девять тысяч "},
  23.             {"", "десять ", "двадцать ", "тридцать ", "сорок ", "пятьдесят ", "шестьдесят ", "семьдесят ", "восемьдесят ", "девяносто "},
  24.             {"", "сто ", "двести ", "триста ", "четыреста ", "пятьсот ", "шестьсот ", "семьсот ", "восемьсот ", "девятьсот "}
  25.     };
  26.  
  27.     public MyAdapter(int size, Context context) {
  28.         this.size = size;
  29.         this.context = context;
  30.     }
  31.  
  32.     @Override
  33.     public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  34.         Context context = parent.getContext();
  35.         int idForItem = R.layout.recycler_view_item;
  36.  
  37.         LayoutInflater inflater = LayoutInflater.from(context);
  38.  
  39.         View view = inflater.inflate(idForItem, parent, false);
  40.  
  41.         MyViewHolder myViewHolder = new MyViewHolder(view);
  42.         return myViewHolder;
  43.     }
  44.  
  45.     @Override
  46.     public void onBindViewHolder(MyViewHolder holder, int position) {
  47.         holder.bind(position + 1);
  48.     }
  49.  
  50.     @Override
  51.     public int getItemCount() {
  52.         return size;
  53.     }
  54.  
  55.     class MyViewHolder extends RecyclerView.ViewHolder {
  56.  
  57.         TextView textView;
  58.  
  59.         public MyViewHolder(View itemView) {
  60.             super(itemView);
  61.             textView = itemView.findViewById(R.id.textView);
  62.             itemView.setOnClickListener(new View.OnClickListener() {
  63.                 @Override
  64.                 public void onClick(View v) {
  65.                     Intent intent = new Intent(context, PagerActivity.class);
  66.                     context.startActivity(intent);
  67.                 }
  68.             });
  69.         }
  70.  
  71.         void bind(int number) {
  72.             textView.setBackgroundResource(R.drawable.background_text_view_recycler2);
  73.             if (number % 2 == 1) {
  74.                 textView.setBackgroundResource(R.drawable.background_text_view_recycler);
  75.                 //textView.setBackgroundColor(Color.YELLOW);
  76.             }
  77.             if(number == 1000000) {
  78.                 textView.setText("миллион");
  79.                 return;
  80.             }
  81.  
  82.             StringBuilder str = new StringBuilder();
  83.  
  84.             int element = 0;
  85.             int count = 0;
  86.             while (number != 0) {
  87.                 element = number % 10;
  88.                 number /= 10;
  89.                 count++;
  90.                 str.insert(0, num[count - 1][element]);
  91.             }
  92.  
  93.  
  94.             String result = str.toString();
  95.  
  96.             Pattern pattern = Pattern.compile("десять \\S* (тысячи|тысяча|тысяч)");
  97.             Matcher matcher = pattern.matcher(result);
  98.             if (matcher.find()) {
  99.                 result = thousands(result);
  100.             }
  101.             pattern = Pattern.compile("десять (\\S{1,})$");
  102.             matcher = pattern.matcher(result);
  103.             if (matcher.find()) {
  104.                 result = dozens(result);
  105.             }
  106.  
  107.             textView.setText(result);
  108.  
  109.         }
  110.  
  111.         public  String thousands(String str) {
  112.             str = str.replaceAll("десять одна тысяча", "одиннадцать тысяч");
  113.             str = str.replaceAll("десять две тысячи", "двенадцать тысяч");
  114.             str = str.replaceAll("десять три тысячи", "тринадцать тысяч");
  115.             str = str.replaceAll("десять четыре тысячи", "четырнадцать тысяч");
  116.             str = str.replaceAll("десять пять тысяч", "пятнадцать тысяч");
  117.             str = str.replaceAll("десять шесть тысяч", "шестнадцать тысяч");
  118.             str = str.replaceAll("десять семь тысяч", "семнадцать тысяч");
  119.             str = str.replaceAll("десять восемь тысяч", "восемнадцать тысяч");
  120.             str = str.replaceAll("десять девять тысяч", "девятнадцать тысяч");
  121.  
  122.             return str;
  123.         }
  124.         public  String dozens(String str) {
  125.             str = str.replaceAll("десять один", "одиннадцать");
  126.             str = str.replaceAll("десять два", "двенадцать");
  127.             str = str.replaceAll("десять три", "тринадцать");
  128.             str = str.replaceAll("десять четыре", "четырнадцать");
  129.             str = str.replaceAll("десять пять", "пятнадцать");
  130.             str = str.replaceAll("десять шесть", "шестнадцать");
  131.             str = str.replaceAll("десять семь", "семнадцать");
  132.             str = str.replaceAll("десять восемь", "восемнадцать");
  133.             str = str.replaceAll("десять девять", "девятнадцать");
  134.  
  135.             return str;
  136.         }
  137.     }
  138. }
Add Comment
Please, Sign In to add comment