3

I need me a little help. I need to use asynctask to display data in ListView. But I don't know how becouse I'm new in Android programming ... thank you very much for any help.

public class Main extends ListActivity {
Button buttonbg;   
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   setContentView(R.layout.listplaceholder);

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    JSONObject json = JSONfunctions.getJSONfromURL("http://10.10.10.10/data.php");

    try{

        JSONArray  ip = json.getJSONArray("ip");

        for(int i=0;i<ip.length();i++){                     
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject e = ip.getJSONObject(i);

            map.put("id",  String.valueOf(i));
            map.put("data1", e.getString("date"));
            map.put("data2", "Location:" +  e.getString("location") + "   Status:" + e.getString("status"));
            mylist.add(map);            
        }       
    }catch(JSONException e)        {
         Log.e("log_tag", "Error parsing data "+e.toString());
    }

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                    new String[] { "data1", "data2" }, 
                    new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);

}}

Nenad
  • 33
  • 1
  • 1
  • 3
  • 1
    Did you [check the documentation](http://developer.android.com/reference/android/os/AsyncTask.html)? – dmon Apr 26 '12 at 14:45
  • 1
    Please post any errors from your logcat and tell us what your specific problem is. – Sam Apr 26 '12 at 14:58
  • I would suggest you to check the document. And [this example](http://www.technotalkative.com/android-google-image-search-api-example-json-parsing-web-api-call-demo/) – Paresh Mayani Apr 27 '12 at 06:54

2 Answers2

8

Try this

new MyAsyncTask.execute("http://10.10.10.10/data.php");

Declare the task as

class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > {

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(String... params) {

        JSONObject json = JSONfunctions.getJSONfromURL(params[0]);

        try {
            JSONArray  ip = json.getJSONArray("ip");

            for (int i=0;i<ip.length();i++) {                     
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = ip.getJSONObject(i);

                map.put("id",  String.valueOf(i));
                map.put("data1", e.getString("date"));
                map.put("data2", "Location:" +  e.getString("location") + "   Status:" + e.getString("status"));
                mylist.add(map);            
            } 
            return mylist
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        ListAdapter adapter = new SimpleAdapter(YourActivity.this, result , R.layout.main, 
                new String[] { "data1", "data2" }, 
                new int[] { R.id.item_title, R.id.item_subtitle });
        YourActivity.this.setListAdapter(adapter); // If Activity extends ListActivity
        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);

    }

Hope it helps.

Joel
  • 14,861
  • 3
  • 27
  • 31
Som
  • 1,514
  • 14
  • 21
2

Do not download any data in your onCreate() - if it takes too long then you will get ANR exception (Activity Not Responding). You should use AsyncTask as in your question. For AsyncTask you have very good example on android site:

http://developer.android.com/reference/android/os/AsyncTask.html

you should put JSONfunctions.getJSONfromURL() inside doInBackground()

and all whats below in onPostExecute()

marcinj
  • 48,511
  • 9
  • 79
  • 100