0

Upon conducting extensive research, I am still troubled in solving this dilemma. In brief, I am trying to have a background image for each listview array of items generated from the JSON data. I have already set the url for the background image in JSON. The JSON function has already been configured, and hence I am already able to populate data into the application from an online source. However, its just the background image I can't pull of. I managed to insert an image, but cant figure out how to set a different background image for each listview section.

I also tried playing around with the XML file, but that has not helped much.

Below is the code for my MainActivity

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
    // Declare Variables
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String LIST_ITEM_NAME = "list_item_name";
    static String COUNTRY = "country";
    static String LIST_ITEM_PRICE = "list_item_price";
    static String LIST_ITEM_BAC = "list_item_bac";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from listview_main.xml
        setContentView(R.layout.listview_main);
        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(MainActivity.this, "Loading.. Please Wait", 5000).show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions
                    .getJSONfromURL("http://dooba.ca/analytics/ed.php");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("list_item");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> retrieve = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    retrieve.put("list_item_bac", jsonobject.getString("list_item_bac"));
                    retrieve.put("list_item_name", jsonobject.getString("list_item_name"));
                    retrieve.put("country", jsonobject.getString("country"));
                    retrieve.put("list_item_price", jsonobject.getString("list_item_price"));

                    // Set the JSON Objects into the array
                    arraylist.add(retrieve);



                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);

        }
    }
}

MainActivity also makes reference to my ListViewAdapter class, and hence,

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

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

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView list_item_name;
        TextView country;
        TextView list_item_price;
        ImageView list_item_bac;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        list_item_name = (TextView) itemView.findViewById(R.id.list_item_name);
        country = (TextView) itemView.findViewById(R.id.country);
        list_item_price = (TextView) itemView.findViewById(R.id.list_item_price);

        // Locate the ImageView in listview_item.xml
        list_item_bac = (ImageView) itemView.findViewById(R.id.list_item_bac);

        // Capture position and set results to the TextViews
        list_item_name.setText(resultp.get(MainActivity.LIST_ITEM_NAME));
        country.setText(resultp.get(MainActivity.COUNTRY));
        list_item_price.setText(resultp.get(MainActivity.LIST_ITEM_PRICE));
        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(MainActivity.LIST_ITEM_BAC), list_item_bac);
        // Capture ListView item click
        itemView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Get the position
                resultp = data.get(position);
                Intent intent = new Intent(context, SingleItemView.class);

                intent.putExtra("list_item_name", resultp.get(MainActivity.LIST_ITEM_NAME));

                intent.putExtra("country", resultp.get(MainActivity.COUNTRY));

                intent.putExtra("list_item_price",resultp.get(MainActivity.LIST_ITEM_PRICE));

                intent.putExtra("list_item_bac", resultp.get(MainActivity.LIST_ITEM_BAC));
                // Start SingleItemView Class
                context.startActivity(intent);

            }
        });
        return itemView;
    }
}

Below is the XML file for listview_item and listview_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="7dp"
    android:paddingBottom="7dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/ranklabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ranklabel" 
        android:textColor="#F0F0F0"/>

    <TextView
        android:id="@+id/list_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/ranklabel" 
        android:textColor="#F0F0F0"/>

    <TextView
        android:id="@+id/countrylabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ranklabel"
        android:text="@string/countrylabel" 
        android:textColor="#F0F0F0"/>

    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/list_item_name"
        android:layout_toRightOf="@+id/countrylabel" 
        android:textColor="#F0F0F0"/>

    <TextView
        android:id="@+id/populationlabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/countrylabel"
        android:text="@string/populationlabel" 
        android:textColor="#F0F0F0"/>

    <TextView
        android:id="@+id/list_item_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/country"
        android:layout_toRightOf="@+id/populationlabel" 
        android:shadowColor="#000000"
        android:shadowDx="-2"
        android:shadowDy="2"
        android:shadowRadius="0.01"
        android:textColor="#f2f2f2"/>

    <ImageView
        android:id="@+id/list_item_bac"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:background="#000000"
        android:alpha="0.8" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/graybac">

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:divider="@drawable/divider"
        android:dividerPadding="12dip"
        android:showDividers="middle"
        />

</RelativeLayout>

Any help or guidance would be much appreciated.

If you need clarification, let me know. Thanks in advance

Update. Below is my JSON file

$arr[list_item] = array(
    array(
        "list_item_bac" => "http://dooba.ca/analytics/boathouse.png",
        "list_item_name" => "The Boat house",
         "country" => "Thursday, July 14",
        "list_item_price" => "5$"
    ),
     array(
        "list_item_bac" => "http://cdn1.sciencefiction.com/wp-content/uploads/2014/05/X-Men1.jpg",
        "list_item_name" => "Movie at ScotiaBank",
        "country" => "bobby",
        "list_item_price" => "10$"
    ),
    array(
        "list_item_bac" => "http://assets.vancitybuzz.com/wp-content/uploads/2014/04/Screen-Shot-2014-04-07-at-7.08.13-PM-880x600.png?a9d4bc",
        "list_item_name" => "Bastille liveshow",
        "country" => "bobby",
         "list_item_price" => "10$"
    ),

     array(
        "list_item_bac" => "http://vancouverfoodster.com/wp-content/uploads/2010/03/Fish-House-afternoon-tea-015.jpg",
        "list_item_name" => "The Fish House In Stanley Park",
        "country" => "bobby",
        "list_item_price" => "10$"
    ),

     array(
        "list_item_bac" => "http://vancouverfoodster.com/wp-content/uploads/2010/03/Fish-House-afternoon-tea-015.jpg",
        "list_item_name" => "test list_item_name",
        "country" => "bobby",
        "list_item_price" => "10$"
    ),

     array(
        "list_item_bac" => "http://vancouverfoodster.com/wp-content/uploads/2010/03/Fish-House-afternoon-tea-015.jpg",
        "list_item_name" => "test list_item_name",
        "country" => "bobby",
        "list_item_price" => "10$"
    ),

     array(
        "list_item_bac" => "http://www.dinemagazine.ca/wp-content/uploads/2012/09/labitoir3.jpg",
        "list_item_name" => "l'abbatoir",
        "country" => "bobby",
        "list_item_price" => "10$"
    ),

     array(
        "list_item_bac" => "http://www.dinemagazine.ca/wp-content/uploads/2012/09/labitoir3.jpg",
        "list_item_name" => "test list_item_name",
        "country" => "bobby",
        "list_item_price" => "10$"
    ),

     array(
        "list_item_bac" => "http://www.dinemagazine.ca/wp-content/uploads/2012/09/labitoir3.jpg",
        "list_item_name" => "test list_item_name",
        "country" => "bobby",
        "list_item_price" => "10$"
    ),

     array(
        "list_item_bac" => "http://www.dinemagazine.ca/wp-content/uploads/2012/09/labitoir3.jpg",
        "list_item_name" => "test list_item_name",
        "country" => "bobby",
        "list_item_price" => "10$"
    )
);

echo json_encode($arr);

As well as the JSON function class

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        // Download JSON data from URL
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jArray;
    }
}

Update 2:

I tried to create a thread that would download the image to later use it has a background

public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView list_item_name;
        TextView country;
        TextView list_item_price;
        ImageView list_item_bac;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);
        RelativeLayout rootRelativeLayout=itemView.findViewById(R.id.rootRelativeLayout);

            new AsyncTask<Void, Void, Void>() {

                            @Override
                            protected Void doInBackground(Void... params) {
                                        Bitmap img = imageLoader.loadImageSync(list_item_bac);

                                return null;
                            }

                            protected void onPostExecute(Void result) {

        rootRelativeLayout.setBackgroundDrawable();
 }

Much appreciated.

Update 3

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

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables

    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView list_item_name;
        TextView country;
        TextView list_item_price;
        ImageView list_item_bac;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);
        RelativeLayout rootRelativeLayout=itemView.findViewById(R.id.rootRelativeLayout);

            new AsyncTask<rootRelativeLayout>() {

                            @Override
                            protected Void doInBackground(Void... params) {
                                        Bitmap img = imageLoader.loadImageSync(list_item_bac);

                                return null;
                            }

                            protected void onPostExecute(Void result) {
                                Drawable d = new BitmapDrawable(Resources, Bitmap);
                            }
            };

        rootRelativeLayout.setBackground(null);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        list_item_name = (TextView) itemView.findViewById(R.id.list_item_name);
        country = (TextView) itemView.findViewById(R.id.country);
        list_item_price = (TextView) itemView.findViewById(R.id.list_item_price);

        // Locate the ImageView in listview_item.xml
        list_item_bac = (ImageView) itemView.findViewById(R.id.list_item_bac);

        // Capture position and set results to the TextViews
        list_item_name.setText(resultp.get(MainActivity.LIST_ITEM_NAME));
        country.setText(resultp.get(MainActivity.COUNTRY));
        list_item_price.setText(resultp.get(MainActivity.LIST_ITEM_PRICE));
        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(MainActivity.LIST_ITEM_BAC), list_item_bac);
        // Capture ListView item click
        itemView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Get the position
                resultp = data.get(position);
                Intent intent = new Intent(context, SingleItemView.class);

                intent.putExtra("list_item_name", resultp.get(MainActivity.LIST_ITEM_NAME));

                intent.putExtra("country", resultp.get(MainActivity.COUNTRY));

                intent.putExtra("list_item_price",resultp.get(MainActivity.LIST_ITEM_PRICE));

                intent.putExtra("list_item_bac", resultp.get(MainActivity.LIST_ITEM_BAC));
                // Start SingleItemView Class
                context.startActivity(intent);

            }
        });
        return itemView;
    }
}

Update 4

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

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables

    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView list_item_name;
        TextView country;
        TextView list_item_price;
        ImageView list_item_bac;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);
        RelativeLayout rootRelativeLayout=(RelativeLayout) itemView.findViewById(R.id.rootRelativeLayout);

        new ImageDownloadTask(rootRelativeLayout,"http://dooba.ca/analytics/ed.php").execute();
        //rootRelativeLayout.setBackground(null);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        list_item_name = (TextView) itemView.findViewById(R.id.list_item_name);
        country = (TextView) itemView.findViewById(R.id.country);
        list_item_price = (TextView) itemView.findViewById(R.id.list_item_price);

        // Locate the ImageView in listview_item.xml
        list_item_bac = (ImageView) itemView.findViewById(R.id.list_item_bac);

        // Capture position and set results to the TextViews
        list_item_name.setText(resultp.get(MainActivity.LIST_ITEM_NAME));
        country.setText(resultp.get(MainActivity.COUNTRY));
        list_item_price.setText(resultp.get(MainActivity.LIST_ITEM_PRICE));
        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(MainActivity.LIST_ITEM_BAC), list_item_bac);
        // Capture ListView item click
        itemView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Get the position
                resultp = data.get(position);
                Intent intent = new Intent(context, SingleItemView.class);

                intent.putExtra("list_item_name", resultp.get(MainActivity.LIST_ITEM_NAME));

                intent.putExtra("country", resultp.get(MainActivity.COUNTRY));

                intent.putExtra("list_item_price",resultp.get(MainActivity.LIST_ITEM_PRICE));

                intent.putExtra("list_item_bac", resultp.get(MainActivity.LIST_ITEM_BAC));
                // Start SingleItemView Class
                context.startActivity(intent);

            }
        });
        return itemView;
    }
    AsyncTask<View,View,View> mytask= new AsyncTask<View,View,View>() {

        @Override
        protected View doInBackground(View... params) {
                    Bitmap img = imageLoader.loadImageSync(list_item_bac);

            return null;
        }
    };

    class ImageDownloadTask extends AsyncTask<View, View, View>
    {
        RelativeLayout mrelativelayout;
        String downloadUrl;
         Bitmap img;
        public ImageDownloadTask(RelativeLayout layout,String url)
        {

            mrelativelayout=layout;
            downloadUrl=url;
        }
        @Override
        protected View doInBackground(View... params) {
            // TODO Auto-generated method stub
            img = imageLoader.loadImageSync(downloadUrl);
            return null;
        }
          protected void onPostExecute(Void result) {
                Drawable d = new BitmapDrawable(context.getResources(), img);
                mrelativelayout.setBackground(d);
            }
    }
    }
user3827788
  • 311
  • 2
  • 9
  • 18
  • where is background image url stored ?? where u have stored the background Image ?? – Software Sainath Jul 11 '14 at 03:56
  • Hi thanks for your response. The background image url is stored within an array in the JSON file. I have updated my initial post with a copy of the file, as well as the android JSON function class. – user3827788 Jul 11 '14 at 04:06

1 Answers1

0
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="7dp"
android:id="@+id/rootRelativeLayout"
android:paddingBottom="7dp"
android:orientation="vertical" >
<TextView
    android:id="@+id/ranklabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ranklabel" 
    android:textColor="#F0F0F0"/>

<TextView
    android:id="@+id/list_item_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/ranklabel" 
    android:textColor="#F0F0F0"/>

<TextView
    android:id="@+id/countrylabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ranklabel"
    android:text="@string/countrylabel" 
    android:textColor="#F0F0F0"/>

<TextView
    android:id="@+id/country"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/list_item_name"
    android:layout_toRightOf="@+id/countrylabel" 
    android:textColor="#F0F0F0"/>

<TextView
    android:id="@+id/populationlabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/countrylabel"
    android:text="@string/populationlabel" 
    android:textColor="#F0F0F0"/>

<TextView
    android:id="@+id/list_item_price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/country"
    android:layout_toRightOf="@+id/populationlabel" 
    android:shadowColor="#000000"
    android:shadowDx="-2"
    android:shadowDy="2"
    android:shadowRadius="0.01"
    android:textColor="#f2f2f2"/>

<ImageView
    android:id="@+id/list_item_bac"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    android:background="#000000"
    android:alpha="0.8" />

In Java Code

 public View getView(final int position, View convertView, ViewGroup parent) {
    // Declare Variables
    TextView list_item_name;
    TextView country;
    TextView list_item_price;
    ImageView list_item_bac;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.listview_item, parent, false);
    RelativeLayout rootRelativeLayout=itemView.findViewById(R.id.rootRelativeLayout);
    rootRelativeLayout.setBackgroundDrawable();
    }

try this

public class ListViewAdapter extends BaseAdapter {

// Declare Variables

Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(Context context,
        ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;
    imageLoader = new ImageLoader(context);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // Declare Variables
    TextView list_item_name;
    TextView country;
    TextView list_item_price;
    ImageView list_item_bac;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.listview_item, parent, false);
    RelativeLayout rootRelativeLayout=itemView.findViewById(R.id.rootRelativeLayout);

    new ImageDownloadTask(rootRelativeLayout,/*Image Download URl here*/).execute();
    //rootRelativeLayout.setBackground(null);
    // Get the position
    resultp = data.get(position);

    // Locate the TextViews in listview_item.xml
    list_item_name = (TextView) itemView.findViewById(R.id.list_item_name);
    country = (TextView) itemView.findViewById(R.id.country);
    list_item_price = (TextView) itemView.findViewById(R.id.list_item_price);

    // Locate the ImageView in listview_item.xml
    list_item_bac = (ImageView) itemView.findViewById(R.id.list_item_bac);

    // Capture position and set results to the TextViews
    list_item_name.setText(resultp.get(MainActivity.LIST_ITEM_NAME));
    country.setText(resultp.get(MainActivity.COUNTRY));
    list_item_price.setText(resultp.get(MainActivity.LIST_ITEM_PRICE));
    // Capture position and set results to the ImageView
    // Passes flag images URL into ImageLoader.class
    imageLoader.DisplayImage(resultp.get(MainActivity.LIST_ITEM_BAC), list_item_bac);
    // Capture ListView item click
    itemView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Get the position
            resultp = data.get(position);
            Intent intent = new Intent(context, SingleItemView.class);

            intent.putExtra("list_item_name", resultp.get(MainActivity.LIST_ITEM_NAME));

            intent.putExtra("country", resultp.get(MainActivity.COUNTRY));

            intent.putExtra("list_item_price",resultp.get(MainActivity.LIST_ITEM_PRICE));

            intent.putExtra("list_item_bac", resultp.get(MainActivity.LIST_ITEM_BAC));
            // Start SingleItemView Class
            context.startActivity(intent);

        }
    });
    return itemView;
}
AsyncTask<View,View,View> mytask= new AsyncTask<View,View,View>() {

    @Override
    protected View doInBackground(View... params) {
                Bitmap img = imageLoader.loadImageSync(list_item_bac);

        return null;
    }

};

class ImageDownloadTask extends AsyncTask<View, View, View>
{
    RelativeLayout mrelativelayout;
    String downloadUrl;
     Bitmap img;
    public ImageDownloadTask(RelativeLayout layout,String url)
    {

        mrelativelayout=layout;
        downloadUrl=url;
    }
    @Override
    protected View doInBackground(View... params) {
        // TODO Auto-generated method stub
        img = imageLoader.loadImageSync(downloadUrl);
        return null;
    }
      protected void onPostExecute(Void result) {
            Drawable d = new BitmapDrawable(context.getResources(), img);
            mrelativelayout.setBackground(d);
        }
}

}

Software Sainath
  • 1,040
  • 2
  • 14
  • 39
  • Before doing this u need to download the image from url and save it int the app directory – Software Sainath Jul 11 '14 at 04:10
  • Hi Software, thanks a lot for your help. The problem is that I want the background image for each array to updated automatically based on the url image link given in the JSON file with the string "list_item_bac" than downloading the image into my local app directory. – user3827788 Jul 11 '14 at 04:21
  • yeahhh before rootRelativeLayout.setBackgroundDrawable(); statement start a thread for downloading Image and then set that image to relativelayoutbackground – Software Sainath Jul 11 '14 at 04:41
  • I admittedly is not an expert at this, and appreciate your contentious help. I tried to apply your above suggestion, and the code of what I have tried to generate can be found in the update section of my initial post (too long to post in the comment). Also the setBackgroundDrawable() method seems to have been deprecated and replaced with setBackground(background or null). If you could further assist me in this matter, I would be thrilled. – user3827788 Jul 11 '14 at 05:04
  • http://stackoverflow.com/a/4560707/1696704 follow this link and create drawable from bitmap and set it to the rootRelativelayout background – Software Sainath Jul 11 '14 at 05:50
  • By the way don't set the background in that way just pass the rootRelativeLayout Reference to asyntask as argument and then set drawable in onPostExecute – Software Sainath Jul 11 '14 at 05:52
  • Thanks for your suggestions. I try to apply them and believe to have made some progress, but is encountering various errors, especially around the asyntask category. I have uploaded the code under update 3 in my initial post. let me know – user3827788 Jul 11 '14 at 08:02
  • check the updated code plzzz learn some basics before starting these kind of concepts – Software Sainath Jul 11 '14 at 08:59
  • I agree that I have to learn more about the basics. From your update, I made the following adjustments: I added a cast (was getting an error) to the RelativeLayout rootRelativeLayout line - RelativeLayout rootRelativeLayout=(RelativeLayout) itemView.findViewById(R.id.rootRelativeLayout); Also i tried to solve this but I am still getting an error in this line Bitmap img = imageLoader.loadImageSync(list_item_bac); it tells me that the value of img is not being used and that the item_bac cannot be resolved into a variable. I provided an updated code in update 4 of my initial post. Thanks! – user3827788 Jul 11 '14 at 15:53