Android - 无法将Spinner值传递给jsonSendRequest

时间:2018-06-03 02:14:47

标签: java android spinner

我是Android开发和Java的新手,所以我想知道是否有人能指出我正确的方向。

我正在编写货币转换器应用并使用Volley请求向API发送请求以检索转换率。

现在我正在将国家/地区硬编码到URL中,这可以在成功检索和显示转换率时起作用。我现在有两个Spinners,目标是检索所选Spinner值的字符串值,然后将这些值用于json请求。

我为两个Spinners创建了两个setOnItemSelectedListener,并将包含字符串值的变量设置为。但是,我得到一个空对象引用。

这就是我所拥有的:

public class MainActivity extends AppCompatActivity {

    RequestQueue rq;
    Double conversionDouble;

    Spinner toSpinner, fromSpinner;
    Spinner toSpinnerText, fromSpinnerText;
    private Button convertBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addItemsOnToSpinner();
        addListenerOnButton();
        jsonSendRequest();

        Button convertBtn = (Button) findViewById(R.id.convertBtn);
        final EditText fromAmountEditText = findViewById(R.id.fromAmountEditText);

        convertBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView toAmountTextView = findViewById(R.id.toAmountTextView);
                DecimalFormat percentageFormat = new DecimalFormat("0.00");
                Double fromAmount = Double.parseDouble(fromAmountEditText.getText().toString());
                String result = "$" + percentageFormat.format(conversionDouble*fromAmount);
                toAmountTextView.setText(result);
            }
        });
        rq = Volley.newRequestQueue(this);

    }

    public void jsonSendRequest() {

        RequestQueue queue = Volley.newRequestQueue(this);
        // String url = "http://free.currencyconverterapi.com/api/v3/convert?q=CAD_USD&compact=ultra";
        String url = "http://free.currencyconverterapi.com/api/v3/convert?q=" + fromSpinnerText + "_" + toSpinnerText  + "&compact=ultra";

        // Request a string response
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        JSONObject jsonobject = null;
                        try {
                            jsonobject = new JSONObject(response);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (jsonobject.has(fromSpinnerText + "_" + toSpinnerText)) {
                            try {
                                conversionDouble = jsonobject.getDouble(fromSpinnerText + "_" + toSpinnerText);
                                // conversionDouble = jsonobject.getDouble(fromSpinnerText + "_" + toSpinnerText);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

// Add the request to the RequestQueue.
        queue.add(stringRequest);
    }

    public void addItemsOnToSpinner(){

        Spinner toSpinner = (Spinner) findViewById(R.id.toSpinner);
        Spinner fromSpinner = (Spinner) findViewById(R.id.fromSpinner);

        List<String> currency = new ArrayList<String>();
        currency.add("USD");
        currency.add("CAD");
        currency.add("CNY");

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
         this, android.R.layout.simple_spinner_item, currency
        );

        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


        toSpinner.setAdapter(dataAdapter);
        fromSpinner.setAdapter(dataAdapter);

        fromSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String fromSpinnerText = (String) parent.getItemAtPosition(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        toSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String toSpinnerText = (String) parent.getItemAtPosition(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    public void addListenerOnButton() {

        Spinner fromSpinner = (Spinner) findViewById(R.id.fromSpinner);
        Spinner toSpinner = (Spinner) findViewById(R.id.toSpinner);
        Button convertBtn = (Button) findViewById(R.id.convertBtn);

    }

}

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您已声明了一些与您的Spinner相关的类变量:

Spinner toSpinner, fromSpinner;
Spinner toSpinnerText, fromSpinnerText;

您还正确定义了onItemSelected()的{​​{1}}方法。

但是,我认为你的意思是让类变量setOnItemSelectedListener实际上是:

Spinner toSpinnerText, fromSpinnerText;

这样你就可以打电话:

String toSpinnerText = "";
String fromSpinnerText= "";

请注意,方法中的变量@Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { fromSpinnerText = (String) parent.getItemAtPosition(position); } 现在使用的是类变量,而不是您在fromSpinnerText方法中定义的局部变量。

另一个onItemSelected()onItemSelected()的{​​{1}}方法也是如此。