parsing image with json to ListView beside text - android

I'm working on an app which require to get the new titles and add thumb images beside them in listView and I don't know how to convet the photos url to images and put in the listView I made here's the code :
I can get the image url but I don't know what to do to add to the listView beside the text I got, any help ?
private class theJob extends AsyncTask<String, Void, ArrayList<HashMap<String, String>>>{
#Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
ListAdapter adapter = new SimpleAdapter(MainActivity.this, result, R.layout.list_item,
new String[] {TAG_CAT_NAME }, new int[] {R.id.label });
setListAdapter(adapter);
Log.d("adapter", "works");
}
#Override
protected ArrayList<HashMap<String, String>> doInBackground(
String... params) {
// TODO Auto-generated method stub
Log.d("Format", params[0]);
Log.d("URL", params[1]);
ArrayList<HashMap<String, String>> cat = new ArrayList<HashMap<String,String>>();
JsonParser jparser = new JsonParser();
Log.d("url", "to the other class");
JSONArray jArray = jparser.getJSONfronUrl(params[1]);
Log.d("json array", "created");
try{
for(int i=0 ; i< jArray.length() ; i++){
JSONObject joob = jArray.getJSONObject(i);
Log.d("jobj", "done");
String title = joob.getString(params[0]);
Log.d(TAG_CAT_NAME, "done");
cat_id = joob.getString(TAG_CAT_ID);
Log.d(TAG_CAT_ID, cat_id);
cat_url.add(i, joob.getString(TAG_CAT_URL)) ;
HashMap<String, String> map = new HashMap<String, String>();
map.put(params[0], title);
cat.add(map);
}
}catch(JSONException e){
e.printStackTrace();
}
Log.d("Going to ADAPTER", "working");
return cat;
}
}

Here is a googe example this will help you what you want :)
Update
Here you will fine more help about Lazy loading of images in list view.

You can make use of SmartImageView, it is a drop-in replacement for Android’s standard ImageView which additionally allows images to be loaded from URLs or the user’s contact address book. Images are cached to memory and to disk for super fast loading.
https://github.com/loopj/android-smart-image-view

Related

JSON to listView in android

I'm trying to put JSON to ListView. I am getting data from http://api.androidhive.info/contacts/ (only using the name field) I am able to get them to array, but im unable to put them into the list,
[NOTE]
however the ListView makes exactly 13 lines for 13 entries (number of names) but the lines are blank.
private class GetJidla extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(TableMenuActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
jidla = jsonObj.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < jidla.length(); i++) {
JSONObject c = jidla.getJSONObject(i);
// String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
// contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
// adding contact to contact list
jidlaList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
TableMenuActivity.this, jidlaList,
android.R.layout.simple_list_item_1, new String[] {TAG_NAME}, new int[] {android.R.id.list,
});
menu = (ListView)findViewById(android.R.id.list);
menu.setAdapter(adapter);
}
and the list is here
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip" >
</ListView>
i need it to be on one screen, in one activity here is the image, the brown lsit is where i need it
I have an exemple to do that:
public class MainActivity extends Activity {
//json string
private String jsonString = "{\"employee\":[{\"emp_name\":\"employee1\",\"emp_no\":\"101700\"},{\"emp_name\":\"employee2\",\"emp_no\":\"101701\"},{\"emp_name\":\"employee3\",\"emp_no\":\"101702\"},"+
"{\"emp_name\":\"employee4\",\"emp_no\":\"101703\"},{\"emp_name\":\"employee5\",\"emp_no\":\"101704\"},{\"emp_name\":\"employee6\",\"emp_no\":\"101705\"},"+
"{\"emp_name\":\"employee7\",\"emp_no\":\"101706\"},{\"emp_name\":\"employee8\",\"emp_no\":\"101707\"},{\"emp_name\":\"employee9\",\"emp_no\":\"101708\"},"+
"{\"emp_name\":\"employee10\",\"emp_no\":\"101709\"},{\"emp_name\":\"employee11\",\"emp_no\":\"101710\"},{\"emp_name\":\"employee12\",\"emp_no\":\"101711\"},"+
"{\"emp_name\":\"employee13\",\"emp_no\":\"101712\"},{\"emp_name\":\"employee14\",\"emp_no\":\"101713\"},{\"emp_name\":\"employee15\",\"emp_no\":\"101712\"}]}";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
ListView listView = (ListView) findViewById(R.id.listView1);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList, android.R.layout.simple_list_item_1, new String[] {"employees"}, new int[] {android.R.id.text1});
listView.setAdapter(simpleAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
List<Map<String,String>> employeeList = new ArrayList<Map<String,String>>();
private void initList(){
try{
JSONObject jsonResponse = new JSONObject(jsonString);
JSONArray jsonMainNode = jsonResponse.optJSONArray("employee");
for(int i = 0; i<jsonMainNode.length();i++){
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("emp_name");
String number = jsonChildNode.optString("emp_no");
String outPut = name + "-" +number;
employeeList.add(createEmployee("employees", outPut));
}
}
catch(JSONException e){
Toast.makeText(getApplicationContext(), "Error"+e.toString(), Toast.LENGTH_SHORT).show();
}
}
private HashMap<String, String>createEmployee(String name,String number){
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}
}
I use this code and it works fine!
Code from JSON Exemple
Here is the very simple example for json to list view
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
download the project and use your own service in MainActivity.java
replace the service URl with your url and format your array accordingly thatz it
happy coding.

skipped 147 frames! The application may be doing too much work on its main thread

I understand the meaning of this error. I found many similar questions here at stackoverflow.com and I have tried to implement the answers those were suggested but still I am getting this error. What I am trying to do is using php web service I am extracting the data from mysql database server and trying to display it in listview using AsyncTask as follows:
class LoadAllProducts extends AsyncTask<String, String, ArrayList<HashMap<String, String>>>
{
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
protected ArrayList<HashMap<String, String>> doInBackground(String... args)
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
Log.d("All Products: ", json.toString());
try {
JSONArray files = json.getJSONArray(TAG_FILES);
for(int i=0;i<files.length();i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = files.getJSONObject(i);
String file_name = e.getString(TAG_FILE_NAME);
String sender = e.getString(TAG_SENDER);
String subject = e.getString(TAG_SUBJECT);
map.put(TAG_SENDER, sender);
map.put(TAG_SUBJECT, subject);
mylist.add(map);
}
} catch (JSONException e)
{
e.printStackTrace();
Log.e("log_tag", "Error parsing data "+e.toString());
}
return mylist;
}
This was suggested in many answers that all the processing should be done in doInBackground function. Now below is the code to display this arraylist in ListView
protected void onPostExecute(String file_url)
{
pDialog.dismiss();
runOnUiThread(new Runnable()
{
public void run()
{
String[] from = { TAG_SENDER, TAG_SUBJECT };
int[] to = { android.R.id.text1, android.R.id.text2 };
ListAdapter adapter = new SimpleAdapter(AllProductsActivity.this, mylist,
android.R.layout.simple_list_item_2, from , to);
setListAdapter(adapter);
}
});
}
Please Help cause first of all I am a beginner of android and I dont have any clue how to solve this problem . Please check my code and let me know the problem.
This type of error will come if your main thread doing so much work. Basically skip frames will happen maximum time when we test our app in emulator, because its already slow and unable to handle huge operation like a device.
I saw a simple problem in your code. We know onPostExecute() runs on MainThread, But still you use runOnUiThread(new Runnable() in onPostExecute() .
You doinbackground method returns an ArrayList , but your onpostexecute hold a string..
Change it as my onPostExecute' parameter that is the ArrayList(mylist)you use in your adapter.
EDIT
class LoadAllProducts extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
protected ArrayList<HashMap<String, String>> doInBackground(String... args){
HttpClient client = new DefaultHttpClient();
HttpGet getData = new HttpGet("your_url");
HttpResponse response = client.execute(getData);
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String data = "";
while((data = br.readLine()) != null){
JsonArray arr = new JsonArray(data);
for(int i=0;i<arr.length();i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = arr.getJSONObject(i);
String file_name = e.getString(TAG_FILE_NAME);
String sender = e.getString(TAG_SENDER);
String subject = e.getString(TAG_SUBJECT);
map.put(TAG_SENDER, sender);
map.put(TAG_SUBJECT, subject);
mylist.add(map);
}
return mylist;
}
protected void onPostExecute(ArrayList<HashMap<String, String>> mylist) {
String[] from = { TAG_SENDER, TAG_SUBJECT };
int[] to = { android.R.id.text1, android.R.id.text2 };
ListAdapter adapter = new SimpleAdapter(AllProductsActivity.this, mylist,
android.R.layout.simple_list_item_2, from , to);
setListAdapter(adapter);
pDialog.dismiss();
}
I'm sorry this is not a super informed answer, but I have 2 suggestions. First, eliminate the runOnUiThread call, because onPostExecute already runs on the ui thread (that is the point of the method). That will not help with any frame skipping, but at least you can get rid of some unneeded code.
Second, create the ListAdapter in doInBackground. I doubt it will make much difference, but you might as well get as much off the UI thread as possible. So, instead of AsyncTask<String, String, ArrayList<HashMap<String, String>>> you will use AsyncTask<String, String, ListAdapter>. That leaves only 2 method calls in onPostExecute, pDialog.dismiss and setListAdapter. You can't get any more efficient than that. If it still skips frames, I would blame the debugger and move on.

android.os.networkexception on Main method [duplicate]

This question already has answers here:
How can I fix 'android.os.NetworkOnMainThreadException'?
(66 answers)
Closed 9 years ago.
Below is my code:
private static final String TAG_TYPE = "movie_type";
private static final String TAG_NAME = "movie_name";
private static final String TAG_LENGTH = "movie_length";
private static final String TAG_SCHEDULES = "movie_schedules";
private static final String TAG_CINEMA = "movie_cinema_number";
private static final String TAG_URL = "movie_image_url";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String readMovieSchedules = readMovieSchedules();
// Hashmap for ListView
ArrayList<HashMap<String, String>> movieList = new ArrayList<HashMap<String, String>>();
JSONArray jsonArray = new JSONArray(readMovieSchedules);
Log.i(MainActivity.class.getName(),
"Number of entries " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i(MainActivity.class.getName(), jsonObject.getString("movie_name"));
// Storing each json item in variable
String name = jsonObject.getString(TAG_NAME);
String type = jsonObject.getString(TAG_TYPE);
String length = jsonObject.getString(TAG_LENGTH);
String cinema = jsonObject.getString(TAG_CINEMA);
String schedules = jsonObject.getString(TAG_SCHEDULES);
String url = jsonObject.getString(TAG_URL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_NAME, name);
map.put(TAG_TYPE, type);
map.put(TAG_LENGTH, length);
map.put(TAG_CINEMA, cinema);
map.put(TAG_SCHEDULES, schedules);
map.put(TAG_URL, url);
// adding HashList to ArrayList
movieList.add(map);
//String strURL = TAG_URL.replaceAll(" ", "%20");
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(MainActivity.this, movieList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_CINEMA, TAG_SCHEDULES },
new int[] { R.id.name, R.id.cinema, R.id.schedules });
//new String[] {},
//new int[] {});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.cinema)).getText().toString();
String description = ((TextView) view.findViewById(R.id.schedules)).getText().toString();
//String url = ((TextView) view.findViewById(R.id.image_)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_CINEMA, cost);
in.putExtra(TAG_SCHEDULES, description);
//in.putExtra(TAG_URL, url);
startActivity(in);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
This code is working on SDK 8, but not working on 17. I'm stuck with this problem. Does anyone here know how to debug this? I'd gladly appreciate your help. thanks.
You can't make network calls on the main thread since API 11 (3.0). Read more here:
http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html
You have to put your code inside an AsyncTask , it is forbidden to download anything on the main UI thread since API 14 I believe.
It would be something like this:
public class someTask extends AsyncTask<String, Void, String> {
public MainActivity activity;
public someTask(MainActivity a)
{
activity = a;
}
#Override
protected String doInBackground(String... urls) {
String stringtoparse=null;
for (String url : urls) {
stringtoparse= readMovieSchedules(url); // getting XML from URL
}
return stringtoparse;
}
#Override
protected void onPreExecute(){
}
#Override
protected void onPostExecute(String readMovieSchedules) {
// Hashmap for ListView
ArrayList<HashMap<String, String>> movieList = new ArrayList<HashMap<String, String>>();
JSONArray jsonArray = new JSONArray(readMovieSchedules);
Log.i(MainActivity.class.getName(),
"Number of entries " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i(MainActivity.class.getName(), jsonObject.getString("movie_name"));
// Storing each json item in variable
String name = jsonObject.getString(TAG_NAME);
String type = jsonObject.getString(TAG_TYPE);
String length = jsonObject.getString(TAG_LENGTH);
String cinema = jsonObject.getString(TAG_CINEMA);
String schedules = jsonObject.getString(TAG_SCHEDULES);
String url = jsonObject.getString(TAG_URL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_NAME, name);
map.put(TAG_TYPE, type);
map.put(TAG_LENGTH, length);
map.put(TAG_CINEMA, cinema);
map.put(TAG_SCHEDULES, schedules);
map.put(TAG_URL, url);
// adding HashList to ArrayList
movieList.add(map);
}
}
You have to modify the readMovieSchedules() to take the url as an argument like this readMovieSchedules(url) and I think it'll work just fine, you would call the task like this:
getMovieSched task = new someTask(MainActivity.this);
task.execute(url);
ASyncTask is specifically made for scenarios like this. It allows you to communicate over the network in a seperate thread without you needing to work with threads.
ASyncTask has these methods :
onPreExecute() : this is invoked before the main processing happens ... one use of it would be to start the progress dialog notifying the user of the transaction.
doInBackground() : this is where you perform data exchange from the network . no UI fiddling around here.
onProgressUpdate() : can be used to notify progress of the transaction
onPostExecute() : dismiss the progressDialogs/bars and update your views from the fetched data!
another solution would be to use runOnUiThread() but this is discouraged!
class nwthread extends AsyncTask<Void,Void,Void>
{
#Override
protected void onPreExecute() {
//progress dialog invoke ( notifies the user about whats going on better than making them stare on a blank screen)
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
//make http request/parse json here
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//dismiss progress dialog
// update the UI here ... ie declare adapters / bind them, update other views
}
}
finally invoke nwthread.execute(); after setContentView();

unable to display image in listview

I am unable to display images but the text is displayed.
I have modified the code from http://mobile.dzone.com/news/android-tutorial-how-parse to the code below.
I would prefer using SimpleAdapter only .Is it possible? As I have implemented the same code as below everywhere.
Or little modifications to the below code would be fine.
How do I display images?
public class MainActivity extends ListActivity {
ArrayList<HashMap<String, String>> mylist;
SimpleAdapter adapter;
ProgressDialog pd ;
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
mylist = new ArrayList<HashMap<String, String>>();
new loadingDisplayClass().execute();
}
public class loadingDisplayClass extends AsyncTask<String, Integer, String> {
protected void onPreExecute() {
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Loading......");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
}
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String url = "https://itunes.apple.com/us/rss/topalbums/limit=50/json";
JSONObject json = JSONfunctions.getJSONfromURL(url);
try {
JSONObject arr2 = json.getJSONObject("feed");
JSONArray arr = arr2.getJSONArray("entry");
for (int i = 0; i < arr.length(); i++) {
JSONObject e1 = arr.getJSONObject(i);
JSONArray jsar = e1.getJSONArray("im:image");
JSONObject jsname = e1.getJSONObject("im:name");
String name = jsname.getString("label");
JSONObject jso = e1.getJSONObject("im:artist");
String lbl = jso.getString("label");
JSONObject e12 = jsar.getJSONObject(0);
String icon = e12.getString("label");
HashMap<String, String> hashmapnew = new HashMap<String, String>();
hashmapnew.put("icons", icon);
hashmapnew.put("name", name);
hashmapnew.put("artist", lbl);
mylist.add(hashmapnew);
publishProgress(((int) ((i + 1 / (float) arr.length()) * 100)));
}
} catch (JSONException e) {
Toast.makeText(getBaseContext(),
"Network communication error!", 5).show();
}
adapter = new SimpleAdapter(getBaseContext(), mylist,
R.layout.list, new String[] {
"icons","name","artist" },
new int[] { R.id.image,R.id.name,R.id.artist });
return null;
}
protected void onProgressUpdate(Integer... integers) {
pd.incrementProgressBy(integers[0]);
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
setListAdapter(adapter);
pd.dismiss();
lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv
.getItemAtPosition(position);
}
});
}
}
}
I am unable to display images but the text is displayed
Images won't display because you are just passing url to the SimpleAdapter.In order to show images you need to first download the images in the url returned above service .
You need to create a custom Adapter and display images by downloading them
Downloading each image and displaying image in imageView takes lot time and as the listview recycle views your images will always download when the list is scrolled. So what you need to do is to use -Universal-Image-Loader or Lazy List to provide automatic downloading and caching images.

using AsyncTask to display data in ListView

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);
}}
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.
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()

Categories

Resources