I am new to Android. I am using Fedor's Lazy Loading List. Is it possible to get all the values from a HashMap (values were retrieved from mysql) and store it to private String[] drinkImages?
String[] drinkImages' values will be passed to adapter Lazy_Adapter.
Or is there any other way to pass the value to Lazy_Adapter?
Code:
Lazy_ListItem.java
public class Lazy_ListItem extends Activity {
ListView list;
Lazy_Adapter adapter;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> drinksList;
// url to get all products list
private static String url_all_drinks = "http://10.0.2.2/restosnapp/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_DRINKS = "drinks";
private static final String TAG_RID = "rid";
private static final String TAG_DRK_ID = "drk_id";
private static final String TAG_DRK_NAME = "drk_name";
private static final String TAG_DRK_DESC = "drk_desc";
private static final String TAG_DRK_PRICE = "drk_price";
private static final String TAG_DRK_AVAIL = "drk_avail";
private static final String TAG_DRK_IMAGE = "drk_image";
// products JSONArray
JSONArray drinks = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
list = (ListView) findViewById(R.id.list);
adapter = new Lazy_Adapter(this, drinkImages);
list.setAdapter(adapter);
// Button b = (Button) findViewById(R.id.button1);
// b.setOnClickListener(listener);
}
#Override
public void onDestroy() {
list.setAdapter(null);
super.onDestroy();
}
/*
* public OnClickListener listener = new OnClickListener() {
*
* #Override public void onClick(View arg0) {
* adapter.imageLoader.clearCache(); adapter.notifyDataSetChanged(); } };
*/
/**
* 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(Lazy_Adapter.this, Lazy_ListItem.class);
* pDialog.setMessage("Loading drinks. Please wait...");
* pDialog.setIndeterminate(false); pDialog.setCancelable(false);
* pDialog.show();
*
* //final ProgressDialog dialog; // dialog =
* ProgressDialog.show(getActivity, "Title", "Message", true); }
*/
/**
* 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.makeHttpRequest(url_all_drinks, "GET",
params);
// Check your log cat for JSON response
Log.d("All Drinks: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
drinks = json.getJSONArray(TAG_DRINKS);
// looping through All Products
for (int i = 0; i < drinks.length(); i++) {
JSONObject c = drinks.getJSONObject(i);
// Storing each json item in variable
String drk_image = c.getString(TAG_DRK_IMAGE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_DRK_IMAGE, drk_image);
// adding HashList to ArrayList
drinksList.add(map);
}
}
} 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();
}
private String[] drinkImages = {
"http://webitprojects.com/restaurant/images/drinks/drk_coffee.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_calamansijuice.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_blackgulaman.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_avocadoshake.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_durianshake.jpg" }; }
Lazy_Adapter.java
public class Lazy_Adapter extends BaseAdapter {
String drk_name, drk_desc, drk_price, drk_avail;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> drinksList;
// url to get all products list
private static String url_all_drinks = "http://10.0.2.2/restosnapp/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_DRINKS = "drinks";
private static final String TAG_RID = "rid";
private static final String TAG_DRK_ID = "drk_id";
private static final String TAG_DRK_NAME = "drk_name";
private static final String TAG_DRK_DESC = "drk_desc";
private static final String TAG_DRK_PRICE = "drk_price";
private static final String TAG_DRK_AVAIL = "drk_avail";
private static final String TAG_DRK_IMAGE = "drk_image";
// products JSONArray
JSONArray drinks = null;
private Activity activity;
private String[] data;
private static LayoutInflater inflater = null;
public Lazy_ImageLoader imageLoader;
public Lazy_Adapter(Activity a, String[] d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new Lazy_ImageLoader(activity.getApplicationContext());
// Hashmap for ListView
drinksList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// ///
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* 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.makeHttpRequest(url_all_drinks, "GET",
params);
// Check your log cat for JSON response
Log.d("All Drinks: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
drinks = json.getJSONArray(TAG_DRINKS);
// looping through All Products
for (int i = 0; i < drinks.length(); i++) {
JSONObject c = drinks.getJSONObject(i);
// Storing each json item in variable
drk_name = c.getString(TAG_DRK_NAME);
drk_desc = c.getString(TAG_DRK_DESC);
drk_price = c.getString(TAG_DRK_PRICE);
drk_avail = c.getString(TAG_DRK_AVAIL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_DRK_NAME, drk_name);
map.put(TAG_DRK_DESC, drk_desc);
map.put(TAG_DRK_PRICE, drk_price);
map.put(TAG_DRK_AVAIL, drk_avail);
// adding HashList to ArrayList
drinksList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
#SuppressWarnings("unused")
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.item, null);
for (Map<String, String> menuItem : drinksList) {
ImageView image = (ImageView) vi.findViewById(R.id.image);
imageLoader.DisplayImage(data[position], image);
TextView tvMenuName = (TextView) vi.findViewById(R.id.tvMenuName);
tvMenuName.setText(drinksList.get(position).get(TAG_DRK_NAME));
TextView tvMenuDesc = (TextView) vi.findViewById(R.id.tvMenuDesc);
tvMenuDesc.setText(drinksList.get(position).get(TAG_DRK_DESC));
TextView tvMenuPrice = (TextView) vi.findViewById(R.id.tvMenuPrice);
tvMenuPrice.setText("P"
+ drinksList.get(position).get(TAG_DRK_PRICE));
TextView tvMenuAvail = (TextView) vi.findViewById(R.id.tvMenuAvail);
tvMenuAvail.setText(drinksList.get(position).get(TAG_DRK_AVAIL));
}
return vi;
}}
I hope you can help me. Thank you.
You can access perticular string on loacation with this code. Here i is an index number which may be 0,1,2 etc.
String yourstring = drinksList.get(i).get(TAG_DRK_IMAGE)
String[] drinkImages = (String[]) drinkList.get(0).values().toArray();
Yes, something like this will work .
public static void main(String[] args) {
Map<String, String> hm = new HashMap<String, String>(); // and yes. I am still using java 6 :(
hm.put("a", "a.jpg");
hm.put("b", "b.jpg");
String[] arr = new String[hm.size()];
hm.values().toArray(arr);
for (String s : arr) {
System.out.println(s);
}
}
O/P:
b.jpg
a.jpg
Try to iterate HashMap and build Array from HashMap value :
public String[] getStringArrayFromMap(HashMap<String,String> map){
String[] array = new String[map.size()];
int i=0;
for (String key : map.keySet()) {
array[i++] = map.get(key);
}
return array;
}
Related
I am just a newbie with Android programming. I am using Fedor's LazyList for my thesis. I've been trying to configure his code but I am not getting any luck. What I would like to do is retrieve data from mysql and place it into the textviews. However, I am only getting the same last data for all textviews and I am not sure why the 3rd one's empty.
My LogCat shows I am getting all the data from the database, so the problem must be the structure of the code.
Code:
Lazy_ListItem.java
public class Lazy_ListItem extends Activity {
ListView list;
Lazy_Adapter adapter;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
// private static String url_all_products =
// "http://10.0.2.2/android_connect/get_all_products.php";
private static String url_all_products = "http://10.0.2.2/restosnapp/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_DRINKS = "drinks";
private static final String TAG_RID = "rid";
private static final String TAG_DRK_ID = "drk_id";
private static final String TAG_DRK_NAME = "drk_name";
private static final String TAG_DRK_DESC = "drk_desc";
private static final String TAG_DRK_PRICE = "drk_price";
private static final String TAG_DRK_AVAIL = "drk_avail";
private static final String TAG_DRK_IMAGE = "drk_image";
// products JSONArray
JSONArray drinks = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
list = (ListView) findViewById(R.id.list);
adapter = new Lazy_Adapter(this, mStrings);
list.setAdapter(adapter);
// Button b = (Button) findViewById(R.id.button1);
// b.setOnClickListener(listener);
}
#Override
public void onDestroy() {
list.setAdapter(null);
super.onDestroy();
}
/*public OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View arg0) {
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
};*/
private String[] mStrings = {
"http://webitprojects.com/restaurant/images/drinks/drk_coffee.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_calamansijuice.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_blackgulaman.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_avocadoshake.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_durianshake.jpg" };
}
Lazy_Adapter.java
public class Lazy_Adapter extends BaseAdapter {
TextView tvMenuName, tvMenuDesc, tvMenuPrice;
String drk_name, drk_desc, drk_price, drk_avail;
int length;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
// private static String url_all_products =
// "http://10.0.2.2/android_connect/get_all_products.php";
private static String url_all_products = "http://10.0.2.2/restosnapp/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_DRINKS = "drinks";
private static final String TAG_RID = "rid";
private static final String TAG_DRK_ID = "drk_id";
private static final String TAG_DRK_NAME = "drk_name";
private static final String TAG_DRK_DESC = "drk_desc";
private static final String TAG_DRK_PRICE = "drk_price";
private static final String TAG_DRK_AVAIL = "drk_avail";
private static final String TAG_DRK_IMAGE = "drk_image";
// products JSONArray
JSONArray drinks = null;
private Activity activity;
private String[] data;
private static LayoutInflater inflater = null;
public Lazy_ImageLoader imageLoader;
public Lazy_Adapter(Activity a, String[] d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new Lazy_ImageLoader(activity.getApplicationContext());
// Loading products in Background Thread
new LoadAllProducts().execute();
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// ///
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* 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.makeHttpRequest(url_all_products, "GET",
params);
// Check your log cat for JSON response
Log.d("All Drinks: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
drinks = json.getJSONArray(TAG_DRINKS);
// looping through All Products
for (int i = 0; i < drinks.length(); i++) {
JSONObject c = drinks.getJSONObject(i);
length = drinks.length();
// Storing each json item in variable
String rid = c.getString(TAG_RID);
String drk_id = c.getString(TAG_DRK_ID);
drk_name = c.getString(TAG_DRK_NAME);
drk_desc = c.getString(TAG_DRK_DESC);
drk_price = c.getString(TAG_DRK_PRICE);
drk_avail = c.getString(TAG_DRK_AVAIL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
// map.put(TAG_RID, rid);
// map.put(TAG_DRK_ID, drk_id);
// map.put(TAG_DRK_NAME, drk_name);
// map.put(TAG_DRK_DESC, drk_desc);
// map.put(TAG_DRK_PRICE, drk_price);
// adding HashList to ArrayList
// productsList.add(map);
}
}
}catch (JSONException e) {
e.printStackTrace();
}
return null;
}}
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.item, null);
// text.setText("Drinks " + position);
//TextView tvMenuName = (TextView) vi.findViewById(R.id.tvMenuName);
ImageView image = (ImageView) vi.findViewById(R.id.image);
imageLoader.DisplayImage(data[position], image);
TextView tvMenuName = (TextView) vi.findViewById(R.id.tvMenuName);
tvMenuName.setText(drk_name);
TextView tvMenuDesc = (TextView) vi.findViewById(R.id.tvMenuDesc);
tvMenuDesc.setText(drk_desc);
TextView tvMenuPrice = (TextView) vi.findViewById(R.id.tvMenuPrice);
tvMenuPrice.setText("P" + drk_price);
TextView tvMenuAvail = (TextView) vi.findViewById(R.id.tvMenuAvail);
tvMenuAvail.setText(drk_avail);
return vi;
}
}
I hope you can help me. Thank you.
UPDATE: Changed code to this and it worked!
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* 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.makeHttpRequest(url_all_products, "GET",
params);
// Check your log cat for JSON response
Log.d("All Drinks: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
drinks = json.getJSONArray(TAG_DRINKS);
// looping through All Products
for (int i = 0; i < drinks.length(); i++) {
JSONObject c = drinks.getJSONObject(i);
// Storing each json item in variable
drk_name = c.getString(TAG_DRK_NAME);
drk_desc = c.getString(TAG_DRK_DESC);
drk_price = c.getString(TAG_DRK_PRICE);
drk_avail = c.getString(TAG_DRK_AVAIL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_DRK_NAME, drk_name);
map.put(TAG_DRK_DESC, drk_desc);
map.put(TAG_DRK_PRICE, drk_price);
map.put(TAG_DRK_AVAIL, drk_avail);
// adding HashList to ArrayList
productsList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.item, null);
for (Map<String, String> menuItem : productsList) {
ImageView image = (ImageView) vi.findViewById(R.id.image);
imageLoader.DisplayImage(data[position], image);
TextView tvMenuName = (TextView) vi.findViewById(R.id.tvMenuName);
tvMenuName.setText(productsList.get(position).get(TAG_DRK_NAME));
TextView tvMenuDesc = (TextView) vi.findViewById(R.id.tvMenuDesc);
tvMenuDesc.setText(productsList.get(position).get(TAG_DRK_DESC));
TextView tvMenuPrice = (TextView) vi.findViewById(R.id.tvMenuPrice);
tvMenuPrice.setText(productsList.get(position).get(TAG_DRK_PRICE));
TextView tvMenuAvail = (TextView) vi.findViewById(R.id.tvMenuAvail);
tvMenuAvail.setText(productsList.get(position).get(TAG_DRK_AVAIL));
}
return vi;
}
It looks like in getView() method you're pulling data out of variables that have been set as class fields, when you need to be pulling it out of the productsList instead.
Try replacing
tvMenuName.setText(drk_name);
with
tvMenuName.setText(productsList.get(position).get(TAG_DRK_NAME));
And so on down the line...
You are seeing the last part of the data in all the TextViews because in your loop in the AsyncTask you set the same variables every time as below. You need to sake these on every iteration of the loop. You could use a HashMap in a List for example. The in the getView method do something like productsList.get(position).get(TAG_DRK_NAME) etc. You actually have most of this commented out.
drk_name = c.getString(TAG_DRK_NAME);
drk_desc = c.getString(TAG_DRK_DESC);
drk_price = c.getString(TAG_DRK_PRICE);
drk_avail = c.getString(TAG_DRK_AVAIL);
I am new to Android.
I want to get the values of drk_image or TAG_DRK_IMAGE from the HashMap and store it to private String[ ] drinkImages. Is this possible?
Code:
Lazy_ListItem.java
public class Lazy_ListItem extends Activity {
ListView list;
Lazy_Adapter adapter;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> drinksList;
// url to get all drinks list
private static String url_all_drinks = "http://10.0.2.2/restosnapp/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_DRINKS = "drinks";
private static final String TAG_DRK_IMAGE = "drk_image";
// drinks JSONArray
JSONArray drinks = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
list = (ListView) findViewById(R.id.list);
adapter = new Lazy_Adapter(this, drinkImages);
list.setAdapter(adapter);
}
#Override
public void onDestroy() {
list.setAdapter(null);
super.onDestroy();
}
/**
* Background Async Task to Load all drinks by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* getting All drinks from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_drinks, "GET",
params);
// Check your log cat for JSON response
Log.d("All Drinks: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// drinks found
// Getting Array of Drinks
drinks = json.getJSONArray(TAG_DRINKS);
// looping through All Drinks
for (int i = 0; i < drinks.length(); i++) {
JSONObject c = drinks.getJSONObject(i);
// Storing each json item in variable
String drk_image = c.getString(TAG_DRK_IMAGE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_DRK_IMAGE, drk_image);
// adding HashList to ArrayList
drinksList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
private String[] drinkImages = {
"http://webitprojects.com/restaurant/images/drinks/drk_coffee.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_calamansijuice.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_blackgulaman.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_avocadoshake.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_durianshake.jpg" }; }
The value of String[ ] drinkImages must be replaced with values from the HashMap (from the database) and so I can pass it to Lazy_Adapter.
I want to get the values from the database instead of just declaring it like this:
private String[] drinkImages = {
"http://webitprojects.com/restaurant/images/drinks/drk_coffee.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_calamansijuice.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_blackgulaman.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_avocadoshake.jpg",
"http://webitprojects.com/restaurant/images/drinks/drk_durianshake.jpg" };
Lazy_Adapter.java
public class Lazy_Adapter extends BaseAdapter {
String drk_name, drk_desc, drk_price, drk_avail;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> drinksList;
// url to get all drinks list
private static String url_all_drinks = "http://10.0.2.2/restosnapp/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_DRINKS = "drinks";
private static final String TAG_RID = "rid";
private static final String TAG_DRK_ID = "drk_id";
private static final String TAG_DRK_NAME = "drk_name";
private static final String TAG_DRK_DESC = "drk_desc";
private static final String TAG_DRK_PRICE = "drk_price";
private static final String TAG_DRK_AVAIL = "drk_avail";
private static final String TAG_DRK_IMAGE = "drk_image";
// drinks JSONArray
JSONArray drinks = null;
private Activity activity;
private String[] data;
private static LayoutInflater inflater = null;
public Lazy_ImageLoader imageLoader;
public Lazy_Adapter(Activity a, String[] d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new Lazy_ImageLoader(activity.getApplicationContext());
// Hashmap for ListView
drinksList = new ArrayList<HashMap<String, String>>();
// Loading drinks in Background Thread
new LoadAllProducts().execute();
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// ///
/**
* Background Async Task to Load all drinks by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* getting All drinks from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_drinks, "GET",
params);
// Check your log cat for JSON response
Log.d("All Drinks: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// drinks found
// Getting Array of Drinks
drinks = json.getJSONArray(TAG_DRINKS);
// looping through All Drinks
for (int i = 0; i < drinks.length(); i++) {
JSONObject c = drinks.getJSONObject(i);
// Storing each json item in variable
drk_name = c.getString(TAG_DRK_NAME);
drk_desc = c.getString(TAG_DRK_DESC);
drk_price = c.getString(TAG_DRK_PRICE);
drk_avail = c.getString(TAG_DRK_AVAIL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_DRK_NAME, drk_name);
map.put(TAG_DRK_DESC, drk_desc);
map.put(TAG_DRK_PRICE, drk_price);
map.put(TAG_DRK_AVAIL, drk_avail);
// adding HashList to ArrayList
drinksList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
#SuppressWarnings("unused")
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.item, null);
for (Map<String, String> menuItem : drinksList) {
ImageView image = (ImageView) vi.findViewById(R.id.image);
imageLoader.DisplayImage(data[position], image);
TextView tvMenuName = (TextView) vi.findViewById(R.id.tvMenuName);
tvMenuName.setText(drinksList.get(position).get(TAG_DRK_NAME));
TextView tvMenuDesc = (TextView) vi.findViewById(R.id.tvMenuDesc);
tvMenuDesc.setText(drinksList.get(position).get(TAG_DRK_DESC));
TextView tvMenuPrice = (TextView) vi.findViewById(R.id.tvMenuPrice);
tvMenuPrice.setText("P"
+ drinksList.get(position).get(TAG_DRK_PRICE));
TextView tvMenuAvail = (TextView) vi.findViewById(R.id.tvMenuAvail);
tvMenuAvail.setText(drinksList.get(position).get(TAG_DRK_AVAIL));
}
return vi;
}}
EDIT:
Output:
This is the output if I have declared private String[] drinkImages. I am hoping to get the same output when getting it from the database.
Emulator Screenshot
Database Screenshot
I am doing this for my thesis. Really appreciate any help.
Thanks a lot~
Because you are using AsyncTask, maybe your task isn't finished yet.
So, you should add your adapter instance and assign it to your listview in onPostExecute method.
Please change your LazyListItem.java looklike below code:
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import android.os.AsyncTask;
import android.widget.ListView;
public class Lazy_ListItem extends Activity {
ListView list;
Lazy_Adapter adapter;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> drinksList;
// url to get all drinks list
private static String url_all_drinks = "http://10.0.2.2/restosnapp/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_DRINKS = "drinks";
private static final String TAG_DRK_IMAGE = "drk_image";
// Your Images Array
private String[] drinkImages;
// drinks JSONArray
JSONArray drinks = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
list = (ListView) findViewById(R.id.list);
new LoadAllProducts().execute();
}
#Override
public void onDestroy() {
list.setAdapter(null);
super.onDestroy();
}
/**
* Background Async Task to Load all drinks by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* getting All drinks from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_drinks, "GET",
params);
// Check your log cat for JSON response
Log.d("All Drinks: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// drinks found
// Getting Array of Drinks
drinks = json.getJSONArray(TAG_DRINKS);
// Define Lenght for Images Array
drinkImages = new String[drinks.length()];
// looping through All Drinks
for (int i = 0; i < drinks.length(); i++) {
JSONObject c = drinks.getJSONObject(i);
// Storing each json item in variable
String drk_image = c.getString(TAG_DRK_IMAGE);
// Add image into Array
drinkImages[i] = drk_image;
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_DRK_IMAGE, drk_image);
// adding HashList to ArrayList
drinksList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
adapter = new Lazy_Adapter(this, drinkImages);
list.setAdapter(adapter);
super.onPostExecute(result);
}
}
I make list that show text from database but the image near items. I need images that get from drawable folder in Android. It means in listview text from database and image from Android.
Code is:
public class Category extends Activity {
// progress dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> categoryList;
private static String url_books = "http://10.0.2.2/project/category.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CATEGORY = "category";
private static final String TAG_CATEGORY_ID = "category_id";
private static final String TAG_CATEGORY_NAME = "category_name";
private static final String TAG_MESSAGE = "massage";
// category JSONArray
JSONArray category = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
Typeface font1 = Typeface.createFromAsset(getAssets(),
"font/bnazanin.TTF");
// Hashmap for ListView
categoryList = new ArrayList<HashMap<String, String>>();
new LoadCategory().execute();
ListView imagelist=(ListView) findViewById(R.id.list_image);
imagelist.setAdapter(new imageview(this));
}
/**
* Background Async Task to Load category by making HTTP Request
* */
class LoadCategory extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Category.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_books, "GET", params);
// Check your log cat for JSON reponse
Log.d("category: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Getting Array of category
category = json.getJSONArray(TAG_CATEGORY);
for (int i = 0; i < category.length(); i++) {
JSONObject c = category.getJSONObject(i);
// Storing each json item in variable
String category_id = c.getString(TAG_CATEGORY_ID);
String category_name = c.getString(TAG_CATEGORY_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CATEGORY_ID,category_id);
map.put(TAG_CATEGORY_NAME, category_name);
// adding HashList to ArrayList
categoryList.add(map);
}
return json.getString(TAG_MESSAGE);
} else {
System.out.println("no category found");
}
} 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() {
ListView lv=(ListView)findViewById(R.id.list_view);
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
Category.this,categoryList,
R.layout.category_item, new String[] { TAG_CATEGORY_NAME},
new int[] { R.id.category_name });
// updating listview
// setListAdapter(adapter);
lv.setAdapter(adapter);
}
});
}
}
In my code, i have text but no image. Any help?
Create a pojo for your needs in list items,You can set pojos values from web or local, then use custom listadapter override getView method according to ur needs. A good explanation here!
I have implement Custom List in which i have stored data -> class->Hash Map->List->Adapter.It Places data successfully and shows me list.But when i click on Item in list.I am unable to retrieve clicked value.my code is
public class asasa extends Activity {
//ListView listView;
Intent intent;
private ProgressDialog pDialog;
//Class Declartion DataHolder
DataHolder obj;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
static final String URL = "http://10.0.2.2/android_connect/get_all_products.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_NAME = "name";
private static final String TAG_Description = "description";
private static final String TAG_URL = "url";
private static final String TAG_Price = "price";
private static final String TAG_class = "DataHolider";
LazyAdapter adapter;
// flag for Internet connection status
Boolean isInternetPresent = false;
// products JSONArray
JSONArray products = null;
// Connection detector class
ConnectionDetector cd;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*ListView l1 = (ListView) findViewById(R.id.sportsList);
l1.setAdapter(new MyCustomAdapter(text, image));
*/
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
LoadAllProducts il = new LoadAllProducts();
il.execute(URL);
}
else
{
// Internet connection is not present
// Ask user to connect to Internet
Toast.makeText(asasa.this, "No Internet Connection You don't have internet connection.", Toast.LENGTH_LONG).show();
}
}
public void longToast(CharSequence message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater=null;
private ArrayList<HashMap<Integer, Object>> data;
//public ImageLoader imageLoader;
int i=0;
public LazyAdapter(Activity a, ArrayList<HashMap<Integer, Object>> d) {
activity = a;
data=d;
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;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView imageview=(ImageView) vi.findViewById(R.id.list_image);
//ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<Integer, Object> song = new HashMap<Integer, Object>();
song = data.get(position);
DataHolder objj=new DataHolder();
objj=(DataHolder) song.get(position);
Log.i("iiiiii "," " +i++);
Log.i("objj.GetName() ",objj.GetName());
Log.i("objj.GetDescription() ",objj.GetDescription());
Log.i("objj.GetPrice() ",objj.GetPrice());
title.setText(objj.GetName());
artist.setText(objj.GetDescription());
duration.setText(objj.GetPrice());
imageview.setImageBitmap(objj.Getimage());
//imageLoader.
return vi;
}
}
class LoadAllProducts extends AsyncTask<String, String, String> {
// creating new HashMap
ArrayList<HashMap<Integer, Object>> productsList=new ArrayList<HashMap<Integer,Object>>();
Bitmap decodedByte;
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(asasa.this);
pDialog.setMessage("Loading products. 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.makeHttpRequest(URL, "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);
Log.i("products ",products.toString());
// looping through All Products
Log.i("LENGTHHHH "," "+products.length());
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
Log.i("ccccccccc ",c.toString());
// Storing each json item in variable
// String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
Log.i("name::",name);
String description = c.getString(TAG_Description);
Log.i("description::",description);
/*String URl = c.getString(TAG_URL);
Log.i("URl",URl);
*/
String price = c.getString(TAG_Price);
Log.i("price",price);
byte[] decodedString = Base64.decode(c.getString(TAG_URL), Base64.DEFAULT);
decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
obj=new DataHolder();
obj.setData(name, description, price, decodedByte);
HashMap<Integer, Object> map = new HashMap<Integer, Object>();
// adding each child node to HashMap key => value
map.put(i, obj);
/*map.put(TAG_Description, description);
map.put(TAG_URL, decodedByte);
map.put(TAG_Price, price);*/
//Log.i("MAP",map.toString());
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
/*Intent i = new Intent(getApplicationContext(),
NewProductActivity.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) {
ListView list;
// dismiss the dialog after getting all products
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
//adapter=new LazyAdapter(a, d)
adapter=new LazyAdapter(asasa.this, productsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("Name---------------"," "+ productsList.get(view.getId()).get(position).getClass().getName());
}
});
if (pDialog.isShowing()) {
pDialog.dismiss();
// progressDialog.setCancelable(true);
}
}
}
public void onPause()
{
super.onPause();
}
public class DataHolder
{
String Name;
String Description;
String Price;
Bitmap image;
public void setData(String Name,String Descipton,String Price,Bitmap iamage)
{
this.Name=Name;
this.Description=Descipton;
this.Price=Price;
this.image=iamage;
}
public String GetName()
{return Name;}
public String GetDescription()
{return Description;}
public String GetPrice()
{return Price;}
public Bitmap Getimage()
{return image;}
}
}
i want help in this part of code
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("Name---------------"," "+ productsList.get(view.getId()).get(position).getClass().getName());
}
});
Q2:why GetView is called infinitely at back-end though list is displayed successfully
i HAVE SOLVED MY PROBLEM THIS WAY
HashMap<String, Object> item = productsList.get(position);
DataHolder Data= new DataHolder();
Data= (DataHolder) item.get(ITEMTITLE);
Log.i("productsListID ", Data.Get_Id())
;
I am trying to consume json data from a server in android app. the format of data which is from a cakePHP web app looks like this;
[{"Chapter":{"id":"1","chapter":"4","chaptertitle":"The Bill of Rights"}}]
and below is the java code am using to achieve my goal;
public class Sheria extends SherlockListFragment {
// Progress Dialog
// private ProgressDialog pDialog;
// private View view;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> chapterList;
// url to get all products list
// private static String url_all_products =
// "http://10.0.2.2/android_projects/sanisani/today_getall.php";
private static String url_chapters = "http://10.0.2.2/constitution/laws/chapters";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CHAPTERS = "Chapter";
private static final String TAG_CHAP = "chapter";
private static final String TAG_CHAP_TITLE = "chaptertitle";
JSONArray chaps = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.sheria, container, false);
new FetchDetails().execute();
return super.onCreateView(inflater, container, savedInstanceState);
}
class FetchDetails extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getSherlockActivity());
pDialog.setMessage("Fetching Data...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
// pDialog.show();
}
#Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
chapterList = new ArrayList<HashMap<String, String>>();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url_chapters);
// Check your log cat for JSON reponse
Log.d("Chapters: ", json.toString());
try {
// Checking for SUCCESS TAG
// int success = json.getInt(TAG_SUCCESS);
// if (success == 1) {
// products found
// Getting Array of Products
chaps = json.getJSONArray(TAG_CHAPTERS);
System.out.println(chaps);
// looping through All Products
for (int i = 0; i < chaps.length(); i++) {
JSONObject c = chaps.getJSONObject(i);
// Storing each json item in variable
String chapter = c.getString(TAG_CHAP);
String chapter_title = c.getString(TAG_CHAP_TITLE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CHAP, chapter);
map.put(TAG_CHAP_TITLE, chapter_title);
// adding HashList to ArrayList
chapterList.add(map);
// getSherlockActivity().finish();
}
// }
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
// Keys used in Hashmap
String[] from = { TAG_CHAP, TAG_CHAP_TITLE };
// Ids of views in listview_layout
int[] to = { R.id.chapter, R.id.chaptertitle };
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity()
.getBaseContext(), chapterList, R.layout.sheriainfo, from,
to);
// Setting the adapter to the listView
setListAdapter(adapter);
}
}
}
When i run the code i get the error JSONArray cannot be converted to JSONObject.
Please help
Because you are getting a JSONArray not a JSONObject, try this:
JSONArray json = jParser.getJSONFromUrl(url_chapters);
and return JSONArray from getJSONFromUrl(String)
and also remove chaps = json.getJSONArray(TAG_CHAPTERS); because your TAG_CHAPTER is not an array.
And in the for loop do
JSONObject c = json.getJSONObject(i);
in place of
JSONObject c = chaps.getJSONObject(i);
Edit
public class Sheria extends SherlockListFragment {
// Progress Dialog
// private ProgressDialog pDialog;
// private View view;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> chapterList;
// url to get all products list
// private static String url_all_products =
// "http://10.0.2.2/android_projects/sanisani/today_getall.php";
private static String url_chapters = "http://10.0.2.2/constitution/laws/chapters";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CHAPTERS = "Chapter";
private static final String TAG_CHAP = "chapter";
private static final String TAG_CHAP_TITLE = "chaptertitle";
JSONArray chaps = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.sheria, container, false);
new FetchDetails().execute();
return super.onCreateView(inflater, container, savedInstanceState);
}
class FetchDetails extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getSherlockActivity());
pDialog.setMessage("Fetching Data...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
// pDialog.show();
}
#Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
chapterList = new ArrayList<HashMap<String, String>>();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
/*
HERE in the method getJSONFromUrl() convert the String u get from server into
JSONArray not JSONObject
*/
JSONArray json = jParser.getJSONFromUrl(url_chapters);
// Check your log cat for JSON reponse
Log.d("Chapters: ", json.toString());
try {
// Checking for SUCCESS TAG
// int success = json.getInt(TAG_SUCCESS);
// if (success == 1) {
// products found
// Getting Array of Products
// chaps = json.getJSONArray(TAG_CHAPTERS);
// System.out.println(chaps);
// looping through All Products
for (int i = 0; i < json.length(); i++) {
JSONObject c = json.getJSONObject(i);
JSONObject jObj = c.getJSONObject("TAG_CHAPTERS ");
// Storing each json item in variable
String chapter = jObj.getString(TAG_CHAP);
String chapter_title = jObj.getString(TAG_CHAP_TITLE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CHAP, chapter);
map.put(TAG_CHAP_TITLE, chapter_title);
// adding HashList to ArrayList
chapterList.add(map);
// getSherlockActivity().finish();
}
// }
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
// Keys used in Hashmap
String[] from = { TAG_CHAP, TAG_CHAP_TITLE };
// Ids of views in listview_layout
int[] to = { R.id.chapter, R.id.chaptertitle };
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity()
.getBaseContext(), chapterList, R.layout.sheriainfo, from,
to);
// Setting the adapter to the listView
setListAdapter(adapter);
}
}
}