Passing data for list view from Asynctask to a custom list adapter class

Monday, July 7, 2014

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.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.dmo.d2d.R;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Inventory extends Activity {


// url to get all products list
private static String url_all_products = "my server url :D";

// Get name and email from global/application context
final String accnt_user_name = globalVariable.getUserName();
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "vinID";
static final String TAG_CAR_NAME = "carName";
static final String TAG_VIN_ID = "vinID";

ListView list;
CustomInventoryList adapter;

JSONObject json;

// Creating JSON Parser object

JSONParser jParser = new JSONParser();

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

// products JSONArray
JSONArray products = null;
// contacts JSONArray
JSONArray contacts = null;
JSONArray account = null;
JSONArray cars = null;
JSONArray user_names = null;
JSONObject jsonObj;

public void onCreate(Bundle savedInstanceState){

//Remove title bar

//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

//set content view AFTER ABOVE sequence (to avoid crash)


new LoadAllProducts().execute();

* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {

protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username_GET", accnt_user_name ));

// getting JSON string from URL
json = jParser.makeHttpRequest(url_all_products, "GET", params);

// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());

runOnUiThread(new Runnable() {

public void run() {

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 vinID = c.getString("vinID");
String name = c.getString("carName").replaceAll("_", " ");
// globalVariable.setUserComment(dealer);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(TAG_CAR_NAME, name);
map.put(TAG_VIN_ID, vinID);

// adding HashList to ArrayList
} else {
// no products found
// Launch Add New product Activity

} catch (JSONException e) {



return null;

protected void onPostExecute(String file_url) {

adapter=new CustomInventoryList(this, productsList); << I CAN'T PASS THIS DATA!!!




I can't pass the data for the list view here is my

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dmo.d2d.R;

import java.util.ArrayList;
import java.util.HashMap;

public class CustomInventoryList extends BaseAdapter{

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
// public ImageLoader imageLoader;

public CustomInventoryList(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//imageLoader=new ImageLoader(activity.getApplicationContext());

public int getCount() {
return data.size();

public Object getItem(int position) {
return position;

public long getItemId(int position) {
return position;

public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
vi = inflater.inflate(R.layout.list_inventory, null);

TextView title = (TextView)vi.findViewById(;
TextView artist = (TextView)vi.findViewById(;
TextView duration = (TextView)vi.findViewById(;
ImageView thumb_image=(ImageView)vi.findViewById(;

HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);

// Setting all values in listview
// duration.setText(song.get(Inventory.KEY_DURATION));
// imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;

I got the code from androidHive, and I am trying to modify it. So in Recap:

  1. I can't transfer the data from the asynctask in my to

  2. How will I be able to solve it?

Thank you in advance. :) Sorry for the long post, but I really need help. :(

Oh btw, I don't have the logcat yet. since I can't pass the data from the async task.