Note that there are some explanatory texts on larger screens.

plurals
  1. POJson Parsing using Async Task and BaseAdapter
    text
    copied!<p>I'm accessing <a href="http://graph.facebook.com/xyz" rel="nofollow">http://graph.facebook.com/xyz</a> and displaying name,id and gender in <code>ListView</code>. I don't know how to call the result from <code>doInBackground</code> and BaseAdapter.</p> <pre><code>public class FbMainActivity extends Activity { Button b; ListView lv; DefaultHttpClient client; HttpGet get; HttpResponse response; HttpEntity entity; ArrayList&lt;FBAccount&gt; al; MyAdapter adapter; TextView tv1, tv2, tv3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fb_main); b = (Button) findViewById(R.id.bFAce); tv1 = (TextView) findViewById(R.id.textView1); tv2 = (TextView) findViewById(R.id.textView2); tv3 = (TextView) findViewById(R.id.textView3); lv = (ListView) findViewById(R.id.list); al = new ArrayList&lt;FBAccount&gt;(); adapter = new MyAdapter(); lv.setAdapter(adapter); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyOperation operation = new MyOperation(); operation.execute(); } }); } private class MyOperation extends AsyncTask&lt;String, Void, String&gt; { ProgressDialog dialog = new ProgressDialog(FbMainActivity.this); @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); dialog.setMessage("Loading...."); dialog.show(); } @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub client = new DefaultHttpClient(); get = new HttpGet("http://graph.facebook.com/somename"); try { response = client.execute(get); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } entity = response.getEntity(); InputStream is = null; try { is = entity.getContent(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } BufferedReader reader = new BufferedReader( new InputStreamReader(is)); StringBuffer buffer = new StringBuffer(); String line = null; do { try { line = reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } buffer.append(line); } while (line != null); String str = buffer.toString(); FBAccount account; JSONObject object; try { object = new JSONObject(str); String id = object.getString("id"); String name = object.getString("name"); String gender = object.getString("gender"); account = new FBAccount(); account.setId(id); account.setName(name); account.setGender(gender); al.add(account); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } adapter.notifyDataSetChanged(); return str; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); dialog.dismiss(); } } private class MyAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return al.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return al.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LinearLayout layout = (LinearLayout) getLayoutInflater().inflate( R.layout.cust_layout, null); String id = al.get(position).getId(); String name = al.get(position).getName(); String gender = al.get(position).getGender(); tv1.setText(id); tv2.setText(name); tv3.setText(gender); return layout; } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload