When user clicks on location element then app populate alert dialog to select location , this locations are coming from php server open .Below is the code i have used.
Declaration
final String locations[] = new String[100];
final String locations_id[] = new String[100];
onPostExecute
jObj = new JSONObject(json);
JSONArray locations_resp = jObj.getJSONArray("Locations");
JSONArray manufacturer_resp = jObj.getJSONArray("Manufacturers");
for(int i=0;i<locations_resp.length();i++)
{
JSONObject c = locations_resp.getJSONObject(i);
int id = c.getInt("id");
String name = c.getString("title");
locations[i]=name;
locations_id[i]=id+"";
//Log.d("Locations","Id ="+id+" name = "+name );
}
onclick Event
location_ele.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select Location");
builder.setItems(locations, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// the user clicked on colors[which]
location_ele.setText(locations[which]);
location=locations_id[which].toString();
}
});
builder.show();
}
});
Screen shot
Observe the screen. locations are coming randomly along with some null values there are only 4 locations coming from API.Please suggest me procedure how to create this locations list dynamically with out Null value please notice i have created Locations as fixed size array.