views:

108

answers:

1

So this is not so much a question as it is a post to an answer that I could not really find a good solution to. I searched quite a bit and I couldn't find anything decent. So after all was said and done, I just had to put my nose to the grindstone and figure it out.

I don't know if the method I'm using is the best way, but it works and i feel that it is a fairly clean solution.

Unfortunately for everyone, I'm not the greatest writer or blogger. So bare with me..

Were going to make a few assumptions here.
1. that you know what a JSONArray is and have already somehow populated the JSONArray with some data.

{"result":[{"ACTIVE":"1","ID":"1","MAX_POPULATION":"1000","NAME":"Server 1","URL":"http://local.orbitaldomination.com/"},{"ACTIVE":"1","ID":"2","MAX_POPULATION":"1000","NAME":"Server 2","URL":"http://server2.orbitaldomination.com/"}]}

This is my JSON code that is populated into my JSONArray.

  1. The next assumption is that you have already created a ListActivity with a ListView element inside it. And that you have created a layout .. if you need more information on how to do that you can reference to Creating Lists Using the ListActivity

Okay, so here is where the real magic is..

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Pretty much ignore this .. it won't have anything to do with the example.
    // Your setContentView should be your layout with your list element.
    setContentView(R.layout.server_selection);

    //psuedo code 
    //JArrayServers = JSONArray that has my data in it.

    //Create a blank ( for lack of better term ) ArrayAdapter
    ArrayAdapter<String> servers = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);

    //Loop though my JSONArray
    for(Integer i=0; i< jArrayServers.length(); i++){
        try{
            //Get My JSONObject and grab the String Value that I want.
            String obj = jArrayServers.getJSONObject(i).getString("NAME");

            //Add the string to the list
            servers.add(obj);
        }catch(JSONException e){

        }
    }
    //Add my Adapter via the setListAdapter function.
    setListAdapter(servers);

    //Display the listView
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
}

Brief Explanation. Keep in mind that I'm only about 3 weeks into java programming.. lol so hope this makes sense.

I am creating a blank ArrayAdapter from what I can tell. I then loop though the array getting my string and inserting it into the adapter as I move along. Finally, at the end of the loop, I am inserting the adapter via the setListAdapter() function.

Pretty simple I think, but took a lot of research to come up with this, from a rookie. I'm sure all you experts out there will have a better way. If you do, for gawd sakes post it somewhere easy to find please! smiles

Hope this helps someone out there..

I can be emailed for questions at [email protected]

Happy Coding,

Marco G. Williams

A: 

Another approach is to create your own adapter class, extending BaseAdapter. This would be useful if, for example, there were more than one piece of data you needed for each ListView row.

CommonsWare
Agreed, could you post an example of that.. The problem is more with that there is very little information on the net of how to do such a thing and that was why I posted this. If you could take a couple minutes and post an example I'm sure all the viewers would love to see how it is best to implement.
Marco Williams