0

I ma trying to populate Spinner from ArrayList where I stored my JSON response.

I cant figure out how to do that. I need spinner with list of items and when item is selected to be able to get values for that item.

Thanks a lot.

@Override
    protected void onPostExecute(Void aVoid) {
       ArrayAdapter<ArrayList<HashMap<String,String>>> adapter = new ArrayAdapter<>(
                MainActivity.this, android.R.layout.simple_spinner_item, array.size());

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

        spinner.setAdapter(adapter);


        super.onPostExecute(aVoid);
    }

Changed it to :

 @Override
    protected void onPostExecute(Void aVoid) {
       ArrayAdapter<ArrayList<HashMap<String,String>>> adapter = new ArrayAdapter<ArrayList<HashMap<String, String>>>(
                MainActivity.this, android.R.layout.simple_spinner_item, Collections.singletonList(valutetList));

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

        spinner.setAdapter(adapter);


        super.onPostExecute(aVoid);
    }

And now i get only one item like:

{"currency_code": "AUD", "median_rate": "4.634211", "selling_rate": "4.648114", "buying_rate": "4.620308", "unit_value": 1}

My full code :

public class MainActivity extends AppCompatActivity {

private String TAG = MainActivity.class.getSimpleName();


ArrayList<HashMap<String, String>> valutetList;
Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = findViewById(R.id.spFrom);
    valutetList = new ArrayList<>();
    new getRates().execute();
}

private class getRates extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        Toast.makeText(MainActivity.this, "Json Data is downloading", Toast.LENGTH_LONG).show();
    }

    @Override
    protected Void doInBackground(Void... voids) {

        HttpHandler httpHandler = new HttpHandler();
        String url = "http://hnbex.eu/api/v1/rates/daily/?date=2018-05-05";
        String jsonStr = httpHandler.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);
        if (jsonStr != null) try {

            JSONArray valute = new JSONArray(jsonStr);
            for (int i = 0; i < valute.length(); i++) {
                HashMap<String, String> map = new HashMap<>();

                JSONObject jsonobject = valute.getJSONObject(i);
                String code = jsonobject.getString("currency_code");
                String selling = jsonobject.getString("selling_rate");
                String buying = jsonobject.getString("buying_rate");
                String median = jsonobject.getString("median_rate");
                String value = jsonobject.getString("unit_value");
                map.put("currency_code", code);
                map.put("selling_rate", selling);
                map.put("buying_rate", buying);
                map.put("median_rate", median);
                map.put("unit_value", value);

                valutetList.add(map);
            }


        } catch (final JSONException e) {
            Log.e(TAG, "Json parsing error: " + e.getMessage());
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Json parsing error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
            });

        }
        else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG).show();
                }
            });
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        ArrayAdapter<ArrayList<HashMap<String, String>>> adapter = new ArrayAdapter<>(
                MainActivity.this, android.R.layout.simple_selectable_list_item, Collections.singletonList(valutetList));

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        super.onPostExecute(aVoid);
    }
}

}

I don't know how to populate spinner.

8
  • why array.size()? Commented Oct 31, 2018 at 12:06
  • Iam not sure, i am getting error without. Commented Oct 31, 2018 at 12:12
  • please post the screenshot of error Commented Oct 31, 2018 at 12:17
  • see this link stackoverflow.com/questions/2784081/… Commented Oct 31, 2018 at 12:21
  • What is the type of valutetList and array? Commented Oct 31, 2018 at 12:46

1 Answer 1

1

Try this solution, may help you. For the json array file, I change it to string.

First Step, create helper class (StackHelper.class)

public class StackHelper {
String code, selling, buying, median, value;

public StackHelper() {
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getSelling() {
    return selling;
}

public void setSelling(String selling) {
    this.selling = selling;
}

public String getBuying() {
    return buying;
}

public void setBuying(String buying) {
    this.buying = buying;
}

public String getMedian() {
    return median;
}

public void setMedian(String median) {
    this.median = median;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

@Override
public String toString () {
    return code;
}}

And then in Activity (StackActivity.class)

public class StackActivity extends AppCompatActivity {

private String TAG = StackActivity.class.getSimpleName();
Spinner spinnerStack;
List <StackHelper> stackHelperList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stack);
    spinnerStack = findViewById(R.id.spFrom);
    spin();

    spinnerStack.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
            String selling = ((StackHelper) spinnerStack.getSelectedItem ()).getSelling ();
            String buying = ((StackHelper) spinnerStack.getSelectedItem ()).getBuying ();
            String median = ((StackHelper) spinnerStack.getSelectedItem ()).getMedian ();
            String value = ((StackHelper) spinnerStack.getSelectedItem ()).getValue ();
            Toast.makeText(StackActivity.this,
                    "Selling : " + selling+"\n" +
                            "Buying : " + buying+"\n" +
                            "Median : " + median+"\n" +
                            "Value : " + value, Toast.LENGTH_SHORT).show();
        }
        public void onNothingSelected(AdapterView<?> arg0) { }
    });
}

private void spin () {
    stackHelperList = new ArrayList<>();

    try {
        String jsonStr = "[{\"unit_value\": 1, \"median_rate\": \"4.656778\", \"currency_code\": \"AUD\", \"buying_rate\": \"4.642808\", \"selling_rate\": \"4.670748\"}, {\"unit_value\": 1, \"median_rate\": \"4.811651\", \"currency_code\": \"CAD\", \"buying_rate\": \"4.797216\", \"selling_rate\": \"4.826086\"}, {\"unit_value\": 1, \"median_rate\": \"0.290560\", \"currency_code\": \"CZK\", \"buying_rate\": \"0.289688\", \"selling_rate\": \"0.291432\"}, {\"unit_value\": 1, \"median_rate\": \"0.994420\", \"currency_code\": \"DKK\", \"buying_rate\": \"0.991437\", \"selling_rate\": \"0.997403\"}, {\"unit_value\": 100, \"median_rate\": \"2.356911\", \"currency_code\": \"HUF\", \"buying_rate\": \"2.349840\", \"selling_rate\": \"2.363982\"}, {\"unit_value\": 100, \"median_rate\": \"5.677144\", \"currency_code\": \"JPY\", \"buying_rate\": \"5.660113\", \"selling_rate\": \"5.694175\"}, {\"unit_value\": 1, \"median_rate\": \"0.767955\", \"currency_code\": \"NOK\", \"buying_rate\": \"0.765651\", \"selling_rate\": \"0.770259\"}, {\"unit_value\": 1, \"median_rate\": \"0.701279\", \"currency_code\": \"SEK\", \"buying_rate\": \"0.699175\", \"selling_rate\": \"0.703383\"}, {\"unit_value\": 1, \"median_rate\": \"6.204487\", \"currency_code\": \"CHF\", \"buying_rate\": \"6.185874\", \"selling_rate\": \"6.223100\"}, {\"unit_value\": 1, \"median_rate\": \"8.395712\", \"currency_code\": \"GBP\", \"buying_rate\": \"8.370525\", \"selling_rate\": \"8.420899\"}, {\"unit_value\": 1, \"median_rate\": \"6.194629\", \"currency_code\": \"USD\", \"buying_rate\": \"6.176045\", \"selling_rate\": \"6.213213\"}, {\"unit_value\": 1, \"median_rate\": \"3.787414\", \"currency_code\": \"BAM\", \"buying_rate\": \"3.776052\", \"selling_rate\": \"3.798776\"}, {\"unit_value\": 1, \"median_rate\": \"7.407537\", \"currency_code\": \"EUR\", \"buying_rate\": \"7.385314\", \"selling_rate\": \"7.429760\"}, {\"unit_value\": 1, \"median_rate\": \"1.736169\", \"currency_code\": \"PLN\", \"buying_rate\": \"1.730960\", \"selling_rate\": \"1.741378\"}]";

        JSONArray jsonArray = null;
        jsonArray = new JSONArray(jsonStr);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonobject = jsonArray.getJSONObject(i);
            String code = jsonobject.getString("currency_code");
            String selling = jsonobject.getString("selling_rate");
            String buying = jsonobject.getString("buying_rate");
            String median = jsonobject.getString("median_rate");
            String value = jsonobject.getString("unit_value");

            StackHelper stackHelper = new StackHelper();
            stackHelper.setCode(code);
            stackHelper.setSelling(selling);
            stackHelper.setBuying(buying);
            stackHelper.setMedian(median);
            stackHelper.setValue(value);
            stackHelperList.add(stackHelper);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    ArrayAdapter<StackHelper> dataAdapter = new ArrayAdapter<StackHelper>(this,
            android.R.layout.simple_selectable_list_item,stackHelperList);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerStack.setAdapter(dataAdapter);

}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.