yudi yudi - 1 month ago 19
Android Question

Android - IF Statement using Spinner while Textview obtained from JSON

Despite following code tutorials I can't find any working code to solve my problem.

I want to use a toast when user chooses an item in spinner they then get this json link

My code:



private void requestJsonObject(){
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, PATH_TO_SERVER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
spinnerData = Arrays.asList(mGson.fromJson(response, DataObject[].class));
//display first question to the user
spinner = (Spinner) findViewById(R.id.spinner);
assert spinner != null;
spinner.setVisibility(View.VISIBLE);
SpinnerAdapter spinnerAdapter = new SpinnerAdapter(MainActivity.this, spinnerData);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String Text = parent.getSelectedItem().toString();
if(Text.equals("ardi halim")){
Toast.makeText(MainActivity.this,"Testing", Toast.LENGTH_LONG).show();
}
else if(Text.equals("indah")){
Toast.makeText(MainActivity.this,"Testing Indah", Toast.LENGTH_LONG).show();
}
}


why my toast not show in the screen when i tap ardi halim??

What is wrong with my code?

thanks to @mmmartinnn who has been advised to be edited

Answer

Basically, if the if-statements do not work, then String Text = parent.getSelectedItem().toString(); is not what you expect, and you should be Log-ing that value to see what it actually is.


Here's an alternative approach to get the data that you've selected on the spinner.

Declare the adapter & listener outside the Volley request.

private SpinnerAdapter spinnerAdapter;
private Spinner spinner;
private List<DataObject> spinnerData = new ArrayList<DataObject>();

private static final GsonBuilder GSON_BUILDER = new GsonBuilder();
private final Gson mGson = GSON_BUILDER.create();

public void onCreate(Bundle b) {
   ...

    //display first question to the user
    spinner = (Spinner) findViewById(R.id.spinner);
    assert spinner != null;
    spinner.setVisibility(View.VISIBLE);

    spinnerAdapter = new SpinnerAdapter(MainActivity.this, spinnerData);
    spinner.setAdapter(spinnerAdapter);

Now, when you set this listener, you can access the data from either spinnerAdapter or spinnerData with getItem or get method, respectively, with the int position to get the selected data.

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // For example
            DataObject selected = spinnerData.get(position);
            String namaDosen = selected.getNama_dosen();

            if(namaDosen.equals("ardi halim")){
                Toast.makeText(MainActivity.this,"Testing", Toast.LENGTH_LONG).show();
            }

        }
    });
}

Also, don't forget to call requestJsonObject() at some point.

And, you simply need to update the spinner data & adapter inside the volley request instead of recreating the adapter and setting the listener for each volley request.

private void requestJsonObject(){
    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, PATH_TO_SERVER, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            spinnerData.clear();
            spinnerData.addAll(Arrays.asList(mGson.fromJson(response, DataObject[].class)));
            spinnerAdapter.notifyDataSetChanged();
        }
    });

    queue.add(stringRequest);
}