Passing Data For List View From Asynctask To A Custom List Adapter Class
I need help on passing data for a custom list view, I can't seem to pass the data from the asynctask to the other java class. here is my code: import android.app.Activity; import
Solution 1:
First, you should really consider moving your Runnable block in doInBackground
to onPostExecute
because that's why it's designed for, UI-thread operations following a background task.
Please note that your AsyncTask
type has changed.
You may also consider making your AsyncTask
independent from your Activity by making it static
. Then you would just pass the arguments the task needs in its constructor, plus a reference to the activity so that it can return the result in onPostExecute
.
The idea:
privateListView mList;
publicvoidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// ...
mlist = ...
newLoadAllProducts(this, username, params, ...); // PASS ALL THE PARAMTERS THE ASYNCTASK NEEDS
}
publicvoidpopulateList(ArrayList<HashMap<String, String>> productsList) {
mList.setAdapter(newCustomInventoryList(this, productsList));
}
staticclassLoadAllProductsextendsAsyncTask<String, String, JSONObject> {
privateString username;
privateList<NameValuePair> params;
privateActivity mActivity;
publicLoadAllProducts(...) {
username = ...;
params = ...;
mActivity = ...;
}
@OverrideprotectedStringdoInBackground(String... args) {
...
// getting JSON string from URL
json = jParser.makeHttpRequest(url_all_products, "GET", params);
return json;
}
protectedvoidonPostExecute(JSONObject json) {
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// ... handle your JSON
}
} catch (JSONException e) {
e.printStackTrace();
}
mActivity.populateList(productsList);
}
}
Post a Comment for "Passing Data For List View From Asynctask To A Custom List Adapter Class"