0 Replies - 524 Views - Last Post: 30 September 2012 - 12:55 PM

#1 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

problem getting data from json to listview

Posted 30 September 2012 - 12:55 PM

This is the code i have so far, but I got stuck getting data from the asynctask to the listview, any help would be apapreciated

public class ScoresMenu extends ListActivity {
public String value;
String result_data, username, score, board_leader;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores_menu);
        
        TabHost host = (TabHost) findViewById(R.id.tabhost); 
        host.setup();

        TabSpec allScoresTab = host.newTabSpec("allTab"); 
        allScoresTab.setIndicator("All Scores", getResources().getDrawable(android.R.drawable.star_on)); 
        allScoresTab.setContent(R.id.all_scores); host.addTab(allScoresTab);
        
        TabSpec friendScoreTab = host.newTabSpec("allTab2"); 
        friendScoreTab.setIndicator("Friend Scores", getResources().getDrawable(android.R.drawable.star_on)); 
        friendScoreTab.setContent(R.id.friends_scores); host.addTab(friendScoreTab);
        
        host.setCurrentTabByTag("allTab");
        
        new ScoreData().execute();
        
        TextView tv = (TextView) findViewById(R.id.two);
      

    }

    
    public class ScoreData extends AsyncTask<String, Integer, JSONObject>{
    

			@Override
			protected JSONObject doInBackground(String... params) {
				// TODO Auto-generated method stub
				InputStream is = null;
		    	StringBuilder sb;
		    	BufferedReader reader;
		    	
		        

		    		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		    		nameValuePairs.add(new BasicNameValuePair("type", "search"));

		    		try {
		    			HttpClient httpclient = new DefaultHttpClient();
		    			HttpPost httppost = new HttpPost("http://10.0.2.2/jamtivia/webservice.php");
		    			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		    			HttpResponse response = httpclient.execute(httppost);
		    			HttpEntity entity = response.getEntity();
		    			is = entity.getContent();
		    		} catch (Exception e) {
		    			Log.e("log_tag", "Error in http connection " + e.toString());
		    		}

		    		// convert response to string
		    		try {
		    			reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);
		    			sb = new StringBuilder();
		    			String line = null;
		    			while ((line = reader.readLine()) != null) {
		    				sb.append(line + "\n");
		    			}
		    			is.close();

		    			result_data = sb.toString();

		    		} catch (Exception e) {
		    			Log.e("log_tag", "Error converting result " + e.toString());
		    		}

		    		//TRANSFER DATA FROM SERVER TO VARIABLES
		    	

		    			JSONObject jArray = null;
						try {
							jArray = new JSONObject(result_data);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} 
		    		
		    			
		    			
				return jArray;
			}

			
			@Override
			protected void onPostExecute(JSONObject result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				JSONArray  jArray = new JSONArray();
				ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

				ListView lv = (ListView) findViewById(R.id.listView1);
				
				for (int i = 0; i < jArray.length(); i++) {
    				// passing data to local variables
    				HashMap<String, String> map = new HashMap<String, String>();
    				JSONObject json_data = jArray.getJSONObject(i);

    				username = "" + json_data.getString("username");
    				score = "" + json_data.getString("score");
    				board_leader = "" + json_data.getString("board_leader");
    				
    				map.put("username",  username);
    				map.put("name", score);
    				map.put("board_leader", board_leader);
    				mylist.add(map);
    			}
				
			}
			  		

    }



Is This A Good Question/Topic? 0
  • +

Page 1 of 1