Android : Individual background image for a listview generated using JSON on Android

on Thursday, July 10, 2014


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


0 comments:

Post a Comment