如何在使用匹配器和正则表达式后传递数组项的原始值

How to Pass Original Value of Array Item after using Matcher and regex

所以在我的应用程序中我有几个微调器,一个微调器给出一个小时费率,

我使用 Matcher、regex 等编辑了微调器中的数据,以便它们在微调器中正确显示

但我无法弄清楚的是我必须将项目 select 上的数组项目的原始字符串传递给 URL 构建器而且我不知道如何在全部,现在已经用谷歌搜索了几个小时

原来如此 对象看起来像这样

7 - 1 - R100

所以我使用匹配器和模式 trim 微调器中的那个值只显示

R100

但是现在我要传

的原值
7 - 1 - R100

到 URI 生成器

这是填充微调器的代码

 private void LoadUserRatesSpinnerData(String url) {


           RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
            StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jsonObject=new JSONObject(response);
                        if (jsonObject.getInt("success") == 1) {
                            JSONArray jsonArray=jsonObject.getJSONArray("Name");
                            for (int i=0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject1=jsonArray.getJSONObject(i);
                                String rates=jsonObject1.getString("UserRate");

                          for(int p=0; p < 100; p++){
                                    final Matcher matcher=Pattern.compile(" - ").matcher(rates);
                                    if (matcher.find()) {
                                        rates=rates.substring(matcher.end()).trim();
                                    }
                                }

                                UserRate.add(rates);



                            }
                        }
                        UserRatesSpinner.setAdapter(new ArrayAdapter<>(IntTimeLog.this, android.R.layout.simple_spinner_dropdown_item, UserRate));
                    } catch (JSONException e) {
                        e.printStackTrace();


                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    error.printStackTrace();
                }
            });

            int socketTimeout=30000;
            RetryPolicy policy=new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            stringRequest.setRetryPolicy(policy);
            requestQueue.add(stringRequest);
        }

微调器的 onItemSelected 代码

      UserRatesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                if (position == 0) {
                    UserRatesSpinner.setClickable(true);
                } else {
                    UserRatesSpinner.setClickable(true);
                    UserRates=UserRatesSpinner.getSelectedItem().toString();
                }
                if (LogHour.getText().toString().contains("Hours Logged")) {
                    LogHour.setText("Log Hours");
                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

目前只有 R100 被传递给 URI 构建器

你应该声明一个新变量,我称之为trimmedRates。就目前而言,您的代码正在覆盖变量 rates

String trimmedRates;   
     if (matcher.find()) {
                           trimmedRates=rates.substring(matcher.end()).trim();
                         }

所以我通过创建另一个 ArrayList 并添加原始 Json 数据来解决这个问题,然后我将匹配器中的数据添加到单独的数组列表,

代码如下

 private void LoadTaskSpinner(String url) {
        final ProgressDialog pd=new ProgressDialog(IntTimeLog.this);
        pd.setMessage("Please Wait..Loading Time Log Data");
        pd.setCanceledOnTouchOutside(false);
        pd.show();
        RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
pd.cancel();
                try {
                    JSONObject jsonObject=new JSONObject(response);
                    if (jsonObject.getInt("success") == 1) {
                        JSONArray jsonArray=jsonObject.getJSONArray("Name");
                        for (int i=0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject1=jsonArray.getJSONObject(i);
                            String task=jsonObject1.getString("TaskName");
                            TaskName.add(task);
                        }
                    }
                    taskSpinner.setAdapter(new ArrayAdapter<>(IntTimeLog.this, android.R.layout.simple_spinner_dropdown_item, TaskName));
                } catch (JSONException e) {
                    e.printStackTrace();


                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                pd.cancel();
               LoadErrorSpinner(ClientsUrl);
                error.printStackTrace();
            }
        });
        int socketTimeout=30000;
        RetryPolicy policy=new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }

然后在项目选择中,我使用 IF 语句将正确的字符串获取到 URL

UserRatesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @SuppressLint("SetTextI18n")
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
        if (position == 1) {
            SelectedSpin = ItemSelect.get(1);

        }
        else{
            UserRatesSpinner.setClickable(true);

        }
        if (position == 2) {
            SelectedSpin = ItemSelect.get(2);

        }
        else {
            UserRatesSpinner.setClickable(true);

        }
        if (position == 3) {
            SelectedSpin = ItemSelect.get(3);
        }
        else{
            UserRatesSpinner.setClickable(true);
        }

        if (LogHour.getText().toString().contains("Hours Logged")) {
            LogHour.setText("Log Hours");
        }


    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
    }
});