implementing the Endless Adapter - android

I get the entire data from the Server by using doInBackground() method as shown below.
class DataLoader extends Activity{
public void onCreate()
{
...............................
new AsyncTask1().execute();
}
class AsyncTask1 extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(DataLoader.this);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();
}
protected String doInBackground(String... args) {
JSONObject json;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1",datafield1));
params.add(new BasicNameValuePair("param2",datafield2));
json = jsonParser.makeHttpRequest(url, "POST", params);
try {
int success = json.getInt(SUCCESS);
if (success == 1) {
products = json.getJSONArray(PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(ID);
String price = c.getString(PRICE);
String name = c.getString(NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(PID, id);
map.put(PRICE, price);
map.put(NAME, name);
..................
// adding HashList to ArrayList
productsList.add(map);
}
return "success";
}
else {
// no materials found for this section
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String msg) {
if( msg != null && msg.equals("success"))
{
progressDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
customadapter=new CustomAdapterList(DataLoader.this, productsList);
listView.setAdapter(adapter);
}
});
}
}
}
As per the above code, I am setting the data to the listview in the onPostExecute() method only after the entire data is loaded. But now I want to implement the CW Endless Adapter with the present code, but as I am new to this, I am unable to get how to move on from here. I included the CWAdapter jar file in the libs folder. Have referred this and searched a lot , but no use. Can some one please help me implementing the endless feature for the data I get?

Basic Idea is to run an AsyncTask when more data is required, that is when uses scrolls to bottom of the list. Here's a quick demo project.

Related

Android: AsyncTask causes app to crash, no error logs printed

i am currently trying a tutorial to connect an android app with a server (link: https://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/).
However, i have difficulties with an AsyncTask which is supposed to load the elements of a database. It causes the app to crash and leaves no error logs in the logcat (also tried "no filter"). I surrounded the execute call with a try catch block as well, but the app still crashes. Any ideas what is causing the error?
here are extracts from the code:
public class AllProductsActivity extends ListActivity {
private static String url_all_products = "https://api.androidhive.info/android_connect/get_all_products.php";
...
#Override
public void onCreate(Bundle savedInstanceState) {
try {
new LoadAllProducts().execute();
}catch (Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
...
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllProductsActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
#Override
protected String doInBackground(String... args) {
// Building Parameters
List<Pair> params = new ArrayList<Pair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
AddNewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllProductsActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
The problem is not with your code implementation. I can see webservice url in your code as
private static String url_all_products = "https://api.androidhive.info/android_connect/get_all_products.php";
The problem is with this web service. That is no longer working. I tested the webservice as you mentioned the link of the tutorial over postman, that is not working any more. Hope you get my point. Feel free to ask me if you have any confusions.

[AsynTask][Restful Client(Android user)][Spring framework] ArrayList of Class from doInBackground to onPostExecute

Following is resful web service display on browser.I need to read these data info andoird as client side as list of table
ArrayList<PickerItemDetail>
//content of http://127.0.0.1:8080/picking
[
{"pickerBatch":1,"sku":"FL45678976543","name":"Coke 1000L","image":"coke.jpg","qty":"8","pickingStatus":"picked","pickedBy":"John","pickedOn":"02/04/2014","locked":"0"},
{"pickerBatch":2,"sku":"FL45678543675","name":"Mineral Water 50L","image":"drinkingWater.jpg","qty":"5","pickingStatus":"picked","pickedBy":"Roy","pickedOn":"02/04/2014","locked":"0"}
]
For android side
public class MainActivity extends ListActivity {
#Override
protected void onStart() {
super.onStart();
new HttpRequestTask().execute();
}
private class HttpRequestTask extends AsyncTask<Void, Void, ArrayList<PickerItemDetail>> {
#Override
protected ArrayList<PickerItemDetail> doInBackground(Void... params) {
try {
final String url = "http://127.0.0.1:8080/picking";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ArrayList<PickerItemDetail> item = restTemplate.getForObject(url, ArrayList.class);
return item;
} catch (Exception e) {
Log.e("MainActivity", e.getMessage(), e);
}
return null;
}
#Override
protected void onPostExecute(ArrayList<PickerItemDetail> item) {
TextView sku = (TextView) findViewById(R.id.sku);
TextView qty = (TextView) findViewById(R.id.qty);
TextView name = (TextView) findViewById(R.id.skuname);
sku.setText(item.get(0).getSku());
qty.setText(item.get(0).getQty());
name.setText(item.get(0).getName());
}
}
}
I would like to read list of data from webservice then display it as a table in android side.
How do I have a proper code to loop over the ArrayList,I think these code not fully correct way,but no idea to continue/make it pretty works.
onPostExecute(ArrayList<>){ item.get(0).getXXX}
Hope someone advice
,Thank
I am not sure what exactly you need but if you want show in listview you can go with
like this.... in your postexecut=sion
add this function loadData();
loadDate() function will be like
private void loadData() {
try {
// looping through All Contacts
contactList.clear();
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String pickerBatch = c.getString("pickerBatch");
String sku = c.getString(sku);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("pickerBatch", pickerBatch);
map.put("sku", sku);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new MySimpleAdapter(Activity.this,
contactList, R.layout.list_item, new String[] {
pickerBatch, sku }, new int[] {
R.id.pickerBatch, R.id.sku });
setListAdapter(adapter);
//
// /
// selecting single ListView item
}
In this above example contactList is your arrayList... Hope it will work

how to load image and pass to Layout using JSON Data?

This is My code,I'm trying to pass the image to listview but i get error.
how can i pass image ? should i download it into local memory first and then send it listview ??
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all, "GET", params);
if(json == null)
System.out.println("NULL");
else{
// Check your log cat for JSON reponse
Log.d("All Developers: ", json.toString());
try {
// Checking for SUCCESS TAG
int status = json.getInt(TAG_STATUS);
if (status == 1) {
// products found
// Getting Array of Products
developers = json.getJSONArray(TAG_all);
// looping through All Products
for (int i = 0; i < developers.length(); i++) {
JSONObject c = developers.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_UID);
String name = c.getString(TAG_NAME);
String image = c.getString(TAG_IMAGE_PATH);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_UID, id);
map.put(TAG_NAME, name);
//map.put(TAG_IMAGE_PATH, image);
// adding HashList to ArrayList
developersList.add(map);
//Iamge Download
}
} else {
System.out.println("No Data");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(getActivity(),
developersList,R.layout.list_data, new String[] { TAG_UID,
TAG_NAME,TAG_IMAGE_PATH},
new int[] { R.id.uid, R.id.name,R.id.image });
// updating listview
lv.setAdapter(adapter);
}
});
}
}
}
Errors
03-30 13:34:03.475: E/BitmapFactory(22326): Unable to decode stream: java.io.FileNotFoundException
You don't have to re-invent the wheel - assuming you have the url to the image you can use nostra13 library for image loading it'll do all the nasty staff for you (cache handling downloading\ saving thumbnail etc..) it also have an exmaple project you can look at to better unnderstand of how to use it.

Using ASync Task to display data in LIstView on postExecute

the problem that im having is kind of weird the printstack of the JSON is correct displaying all the element in the table the way that it should the same as for the method String doInBackground(String... args) the problem is in the postExecute method that is displaying the same element in all the element of the listview "the last element in the row of the table to be exact can someone tell me what i did wrong " thank you for your time bellow you will find the class that im talking about thank you
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ProDentActivity.this);
pDialog.setMessage("Loading Balance. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(balanceURL, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
amount = json.getJSONArray(TAG_BALANCE);
// looping through All Products
for (int i = 0; i < amount.length(); i++) {
JSONObject c = amount.getJSONObject(i);
// Storing each json item in variable
String amount = c.getString(TAG_AMOUNT);
String createdat = c.getString(TAG_CREATEDAT);
//String userid = c.getString(TAG_USERID);
// adding each child node to HashMap key => value
map.put(TAG_AMOUNT, amount);
map.put(TAG_CREATEDAT, createdat);
//map.put(TAG_USERID , userid);
// adding HashList to ArrayList
amountlist.add(map);
}
} else {
// no balance found
// Launch Add New balance Activity
Intent i = new Intent(getApplicationContext(),
ProDentActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
ProDentActivity.this, amountlist,
R.layout.list_item, new String[] { TAG_AMOUNT,TAG_CREATEDAT},
new int[] { R.id.amount, R.id.createdat });
// updating listview
setListAdapter(adapter);
}
});
}
}
JSON:
{"balance":[{"amount":"50000","created_at":"2012-12-15 02:39:13"},{"amount":"50000","created_at":"2012-12-16 15:29:03"},{"amount":"30000","created_at":"2012-12-17 19:38:07"}],"success":1}
...and the method returns
12-18 02:29:05.797: D/All Products:(885): {"success":1,"balance":[{"amount":"50000","created_at":"2012-12-15 02:39:13"},{"amount":"50000","created_at":"2012-12-16 15:29:03"},{"amount":"30000","created_at":"2012-12-17 19:38:07"}]}
problem fixed i found what i have been doing wrong. the problem was that i didn't define my hash map in the method protected String doInBackground(String... args)
i was defining it in the listactivity anw thank you for your time and help
bellow code to be replaced with the old one
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
amount = json.getJSONArray(TAG_BALANCE);
// looping through All Products
for (int i = 0; i < amount.length(); i++) {
JSONObject c = amount.getJSONObject(i);
// Storing each json item in variable
String amount = c.getString(TAG_AMOUNT);
String createdat = c.getString(TAG_CREATEDAT);
//String userid = c.getString(TAG_USERID);
// adding each child node to HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_AMOUNT, amount);
map.put(TAG_CREATEDAT, createdat);
//map.put(TAG_USERID , userid);
// adding HashList to ArrayList
amountlist.add(map);
}
} else {
// no balance found
// Launch Add New balance Activity
Intent i = new Intent(getApplicationContext(),
ProDentActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

Is it Possible to get results of AsyncTask as an Arraylist Hashmap

I have at least three activities in my app right now that use an AsyncTask to return JSON results into an ListView. I've started work on the app, but another person will take over development as soon as he gets the basics down, so I want to try and make things as easy to use as possible. This means that I'm trying to turn as much repeatable code into callable functions as possible, so instead of needing to copy/paste/reuse 30-40 lines of code each time they need query a webservice, they can just pass in parameters to a function.
Currently, I have the following in an activity that pulls a list of gym classes from a mysql database via a php webservice:
class LoadAllClasses extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
// pDialog = new ProgressDialog(Checkin.this);
// pDialog.setMessage("Loading products. Please wait...");
// pDialog.setIndeterminate(false);
// pDialog.setCancelable(false);
// pDialog.show();
}
/**
* getting All products from url
* */
#Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", getclasses_tag));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(SmashGyms.WEBSERVICE_URL,
"POST", params);
// Check your log cat for JSON response
Log.d("CheckinDialog", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// classes found
// Getting Array of Classes
classes2 = json.getJSONArray(TAG_CLASSES);
// looping through All Classes
for (int i = 0; i < classes2.length(); i++) {
JSONObject c = classes2.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_CLASSID);
String name = c.getString(TAG_CLASSNAME);
//String day = c.getString(TAG_DAY);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CLASSID, id);
map.put(TAG_CLASSNAME, name);
//map.put(TAG_DAY, day);
// adding HashList to ArrayList
allclasseslist.add(map);
Log.d("map: ", map.toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
#Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
runOnUiThread(new Runnable() {
#Override
public void run() {
/**
* Updating parsed JSON data into ListView
* */
adapter = new SimpleAdapter(CheckinDialog.this,
allclasseslist, R.layout.checkin_item,
new String[] { TAG_CLASSID, TAG_CLASSNAME },
new int[] { R.id.pid, R.id.name });
setListAdapter(adapter);
}
});
//pDialog.dismiss();
// updating UI from Background Thread
}
}
I'd like to move this to another class that I have, called "WebServiceTasks", so that I can call something like this in the activity's OnCreate():
allclasseslist = new ArrayList<HashMap<String, String>>();
allclasseslist = new WebServiceTasks.LoadAllClasses().get();
adapter = new SimpleAdapter(CheckinDialog.this,
allclasseslist, R.layout.checkin_item,
new String[] { TAG_CLASSID, TAG_CLASSNAME },
new int[] { R.id.pid, R.id.name });
setListAdapter(adapter);
While I've tried this, I get a number of errors related to either defining the asyncTask wrong, or other things not matching up.
Here is what I've tried putting in my "WebServiceTasks" class:
public static class LoadAllClasses extends
AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> allclasseslist;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CLASSES = "classes";
private static final String TAG_CLASSID = "id";
private static final String TAG_CLASSNAME = "class";
private static final String getclasses_tag = "getclasses";
JSONArray classes2 = null;
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
// pDialog = new ProgressDialog(Checkin.this);
// pDialog.setMessage("Loading products. Please wait...");
// pDialog.setIndeterminate(false);
// pDialog.setCancelable(false);
// pDialog.show();
}
/**
* getting All classes from url
* */
#Override
protected ArrayList<HashMap<String, String>> doInBackground(
String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", getclasses_tag));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(SmashGyms.WEBSERVICE_URL,
"POST", params);
// Check your log cat for JSON response
Log.d("CheckinDialog", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// classes found
// Getting Array of Classes
classes2 = json.getJSONArray(TAG_CLASSES);
// looping through All Classes
for (int i = 0; i < classes2.length(); i++) {
JSONObject c = classes2.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_CLASSID);
String name = c.getString(TAG_CLASSNAME);
//String day = c.getString(TAG_DAY);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CLASSID, id);
map.put(TAG_CLASSNAME, name);
//map.put(TAG_DAY, day);
// adding HashList to ArrayList
allclasseslist.add(map);
Log.d("map: ", map.toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return allclasseslist;
}
/**
* After completing background task Dismiss the progress dialog
* **/
#Override
protected void onPostExecute(
ArrayList<HashMap<String, String>> allclasses) {
// dismiss the dialog after getting all products
//pDialog.dismiss();
// updating UI from Background Thread
}
}
Is this possible, and if so, what am I doing wrong?
Well, you are trying to use get() method of AsyncTask which is very expensive because it blocks the UI until your onPostExecute() is completed. I would insist you to fire a BroadCastReceiver in onPostExecute() to update your UI or create and Interface and pass the result to your Activity using that interface in your onPostExecute(). I had just created a small demo for using BroadCastReceiver and Interface for passing result from onPostExecute() to your Activity. You can find a demo source from my github here.
You Can Create A Bean class to store all the values from async task so that your can receive it to another class my friend
For anyone trying to replicate this, Here is how I solved this, using Lalit and Samir's examples:
In my Activity:
public class CheckinDialog extends ListActivity implements
AsyncTaskCompleteListener {
WebServiceTasks.LoadAllClasses objAsyncTask = new WebServiceTasks.LoadAllClasses(
this);
objAsyncTask.execute();
#Override
public void onTaskComplete(ArrayList<HashMap<String, String>> allclasseslist) {
// TODO Auto-generated method stub
adapter = new SimpleAdapter(CheckinDialog.this, allclasseslist,
R.layout.checkin_item, new String[] { TAG_CLASSID,
TAG_CLASSNAME }, new int[] { R.id.pid, R.id.name });
setListAdapter(adapter);
Log.d("OnTaskComplete", "taskcomplete");
}
In an interface called "AsyncTaskCompleteListener":
public interface AsyncTaskCompleteListener {
void onTaskComplete(ArrayList<HashMap<String, String>> allclasseslist);
}
And the seperate WebServiceTasks Class:
public static class LoadAllClasses extends
AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
JSONParser jParser = new JSONParser();
private final AsyncTaskCompleteListener callback;
private final Activity activity;
public LoadAllClasses(Activity act) {
this.activity = act;
this.callback = (AsyncTaskCompleteListener) act;
}
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CLASSES = "classes";
private static final String TAG_CLASSID = "id";
private static final String TAG_CLASSNAME = "class";
private static final String getclasses_tag = "getclasses";
JSONArray classes2 = null;
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting All classes from url
* */
#Override
protected ArrayList<HashMap<String, String>> doInBackground(
String... args) {
// Building Parameters
ArrayList<HashMap<String, String>> allclasseslist = null;
allclasseslist = new ArrayList<HashMap<String, String>>();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", getclasses_tag));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(SmashGyms.WEBSERVICE_URL,
"POST", params);
// Check your log cat for JSON response
Log.d("CheckinDialog", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// classes found
// Getting Array of Classes
classes2 = json.getJSONArray(TAG_CLASSES);
Log.d("JSONArray", json.getJSONArray(TAG_CLASSES)
.toString());
// looping through All Classes
for (int i = 0; i < classes2.length(); i++) {
JSONObject c = classes2.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_CLASSID);
String name = c.getString(TAG_CLASSNAME);
//String day = c.getString(TAG_DAY);
// creating new HashMap
final HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CLASSID, id);
map.put(TAG_CLASSNAME, name);
//map.put(TAG_DAY, day);
// adding HashList to ArrayList
allclasseslist.add(map);
//Log.d("map: ", map.toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return allclasseslist;
}
/**
* After completing background task Dismiss the progress dialog
* **/
#Override
protected void onPostExecute(
ArrayList<HashMap<String, String>> allclasseslist) {
super.onPostExecute(allclasseslist);
// dismiss the dialog after getting all classes
callback.onTaskComplete(allclasseslist);
}
}
Thanks to all for the quick help. This ended up saving over 120 lines of repeating code in each activity that used this code.

Categories

Resources