java
sql
iphone
c
database
xcode
ruby-on-rails
objective-c
eclipse
html5
facebook
oracle
cocoa
tsql
apache
mvc
asp
api
postgresql
dom
public class JsonExampleActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.fetchTwitterPublicTimeline())); } public ArrayList<String> fetchTwitterPublicTimeline() { ArrayList<String> listItems = new ArrayList<String>(); try { URL twitter = new URL("your url here"); URLConnection tc = twitter.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader( tc.getInputStream())); String line; while ((line = in.readLine()) != null) { JSONObject ja = new JSONObject(line); /* //for (int i = 0; i < ja.length(); i++) { JSONObject jo = ja.getJSONObject("_api_error"); listItems.add(jo.getString("name"));*/ JSONArray jobj=ja.getJSONArray("artists"); //JSONArray ja = new JSONArray(line); for (int i = 0; i < jobj.length(); i++) { JSONObject jo = jobj.getJSONObject(i); listItems.add(jo.getString("artist_name")); } // } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return listItems; } }
Simplest thing that I would do is to put JSONArray in some ArrayList and use it in arrayadapter
You can add all json objects into a list, and then load list from there on, but as you have prepared a method which returns a single string. you can use following:
Use
String[] tokens=returnString.split("\n\r"); ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1, tokens); listView.setAdapter(adapter);
take a list of string and store the ContactName in that
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, returnString));
here returnString will be of arraylist.
make getServerData return arraylist.
Learn the Basics of an Custom Adapter then...
Whenever you want to do processing with the views in a ListView you need to create a custom adapter that will handle your logic implementation and pass that information to the views as necessary.
I'd make sure you are making your requests in a background thread(eg async task or intent service) or your app may crash if it takes to long.
As for the list activity
http://www.vogella.de/articles/AndroidListView/article.html
This tutorial will show you how to create a custom model and populate it into a list view with custom xml.