CodeAssasins CodeAssasins - 4 months ago 25
Android Question

set spinner blank if jsonarray is null

I've 2 APIs and two spinners: one for state's list and another for city's list. In some cases the city list's JSONArray might be null. So I want to check in volley's JSON parsing if its null then set some default message in spinner. But its not showing the default message. Instead its showing the city list of the state which is by default set. Is there any solution for this?



// null city list

{
"citylist": []
}

// JSON for state list

{
"statelist": [
{
"state_id": "1",
"state_name": "West bengal"
},
{
"state_id": "3",
"state_name": "Himachal Pradesh"
},
{
"state_id": "4",
"state_name": "Maharashtra"
},
{
"state_id": "11",
"state_name": "Queensland"
}
]
}

public class MainActivity extends AppCompatActivity {

private static final String STATE = "http://example.com/ubooktoday/android/showstatebycountry";
private static final String CITY = "http://example.com/ubooktoday/android/showcitybystate";
Spinner spin1, spin2;
String stateid, cityid, zipid;
ArrayList<String> namelist, idlist;
ArrayAdapter<String> adapter;
HashMap<String,String> spinnerMap1, spinnerMap2;

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

spin1=(Spinner)findViewById(R.id.spin1);
spin2=(Spinner)findViewById(R.id.spin2);

namelist = new ArrayList<String>();
idlist = new ArrayList<String>();

loadstate();

spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String statename = spin1.getSelectedItem().toString();
stateid = spinnerMap1.get(statename);
//Toast.makeText(getApplicationContext(), stateid, Toast.LENGTH_SHORT).show();
loadcity();
}

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

}
});

spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String cityname = spin2.getSelectedItem().toString();
cityid = spinnerMap2.get(cityname);
Toast.makeText(getApplicationContext(), cityid, Toast.LENGTH_SHORT).show();
}

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

}
});



}



private void loadstate() {

if(namelist!=null )namelist.clear();
if(idlist!=null )idlist.clear();


StringRequest stringRequest = new StringRequest(Request.Method.POST, STATE,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {

try {

JSONObject jObj = new JSONObject(response);

JSONArray jsonArray = jObj.getJSONArray("statelist");
for(int i=0; i<jsonArray.length(); i++){
JSONObject obj = jsonArray.getJSONObject(i);


namelist.add(obj.getString("state_name"));
idlist.add(obj.getString("state_id"));

String[] spinnerArray = new String[idlist.size()];
spinnerMap1 = new HashMap<String, String>();
for (int j = 0; j < idlist.size(); j++)
{
spinnerMap1.put(namelist.get(j),idlist.get(j));
spinnerArray[j] = namelist.get(j);
}

adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, spinnerArray);

spin1.setAdapter(adapter);
}

} catch (JSONException e) {
e.printStackTrace();
}


}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("country_id", "2");
return params;
}

};

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}

private void loadcity() {

if(namelist!=null )namelist.clear();
if(idlist!=null )idlist.clear();

StringRequest stringRequest = new StringRequest(Request.Method.POST, CITY,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {

try {
JSONObject jObj = new JSONObject(response);

JSONArray jsonArray = jObj.getJSONArray("citylist");

if(jsonArray.equals("null")){
namelist.add("No Items");

adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, namelist);
spin2.setAdapter(adapter);
}else{

for(int i=0; i<jsonArray.length(); i++){

JSONObject obj = jsonArray.getJSONObject(i);


namelist.add(obj.getString("city_name"));
idlist.add(obj.getString("city_id"));

String[] spinnerArray = new String[idlist.size()];
spinnerMap2 = new HashMap<String, String>();
for (int j = 0; j < idlist.size(); j++)
{
spinnerMap2.put(namelist.get(j),idlist.get(j));
spinnerArray[j] = namelist.get(j);
}

adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, spinnerArray);
spin2.setAdapter(adapter);
}

}





} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("state_id", stateid);
return params;
}

};

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}


}




Answer

Use below code for checking json array condition,

  if(jsonArray == null || jsonArray.equals("null") || jsonArray.length <= 0)
  {
       namelist.add("No Items");

       adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, namelist);
       spin2.setAdapter(adapter);
  }
Comments