0

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 enter image description here

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.

manikanta g
  • 298
  • 2
  • 4
  • 17

1 Answers1

0

In your post execute you need a condition when looping on locations_resp that checks when a location is null or empty so you won't add it to your location array.

I would suggest to work with List instead of array so your listview matches the size of the datasource (for instance show a list of 2 locations instead a list of 10 locations where 8 are empty).

List<String> locations = new ArrayList<String>();
List<String> locationsId = new ArrayList<String>();

for (JSONObject c : locations_resp)
{
    int id = c.getInt("id");
    String name = c.getString("title");
    if(name != null && name.length > 0)
    {
        locations.add(name);
        locationsId.add(String.valueOf(id));
    }
}

enter image description here

I'll also suggest to have a local Location representation instend of having 2 array so you can do something like :

List<Location> locations = new ArrayList<Location>();

for (JSONObject c : locations_resp)
{
    int id = c.getInt("id");
    String name = c.getString("title");
    if(name != null && name.length > 0)
    {
        locations.add(new Location(name, id));
    }
}

and work with the Location object.

Kalem
  • 1,132
  • 12
  • 33
  • I tried your code but new error comes at `builder.setItems(locations.toArray(), new DialogInterface.OnClickListener() ` saying "Cast 1st parameter to 'java.lang.CharSequence[]'" @Kalem – manikanta g Apr 24 '15 at 09:40
  • My bad, to use the second suggestion you need to define an Adapter and use `AlertDialog.Builder.setAdapter(ListAdapter adapter, DialogInterface.OnClickListener listener)` instead of `AlertDialog.Builder.setItems(CharSequence[] items, DialogInterface.OnClickListener listener)`. So if you don't wanna define an Adapter (you should if you want to customise the rows view) stick with the first suggestion. – Kalem Apr 24 '15 at 09:46
  • I tried your first suggestion not second one @Kalem – manikanta g Apr 24 '15 at 09:53
  • Then you have a problem when casting the List : try `builder.setItems(locations.toArray(new CharSequence[locations.size()]), new DialogInterface.OnClickListener()` see http://stackoverflow.com/questions/3032342/arrayliststring-to-charsequence – Kalem Apr 24 '15 at 10:08
  • and one more thing actually dialog open when taps on edittext but not on focus gain how can i do this so that it will open on focus also – manikanta g Apr 24 '15 at 10:18
  • This is not related to the question. Maybe you should ask a new question after searching a little on stack ;) – Kalem Apr 24 '15 at 10:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76160/discussion-between-manikanta-g-and-kalem). – manikanta g Apr 24 '15 at 10:33