I have created a listview with headers but I am unable to group items that fall under the same header.
Can someone advise me on what I should do?
Below are the codes to display the items into the listview:
public class OrganizationList extends Activity implements OnClickListener {
ListView lv;
SimpleAdapter simpleAdapter;
ArrayList<HashMap<String, String>> videosList;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
JSONArray videos = null;
// url to get all videos
private static String url_video = "http://10.0.2.2/android_connect/get_organization.php";
//JSON Nodes
private static final String TAG_POSITION = "position";
private static final String TAG_URL = "URL";
private static final String TAG_MEMBER = "name";
private static final String TAG_SUCCESS = "success";
private static final String TAG_NAME = "members";
protected static final int GET_INTENT_CODE = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
Button refresh = (Button)findViewById(R.id.btn_refresh);
refresh.setOnClickListener(this);
TextView header = (TextView)findViewById(R.id.text_header);
header.setText("Videos (视频)");
ImageButton home = (ImageButton)findViewById(R.id.btn_home);
home.setOnClickListener(this);
// Hashmap for ListView
videosList = new ArrayList<HashMap<String, String>>();
//Get adapter
simpleAdapter = new SimpleAdapter(OrganizationList.this, videosList, R.layout.list_date_item, new String[] {TAG_POSITION, TAG_MEMBER},
new int[] { R.id.text_header, R.id.name});
//Get Listview
lv = (ListView) findViewById(R.id.lst_name);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String videoName = ((TextView) view.findViewById(R.id.name)).getText()
.toString();
Log.d("row click:", videoName);
}
});
// Loading videos in Background Thread
new LoadAllVideos().execute();
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllVideos extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(OrganizationList.this);
pDialog.setMessage("Loading" + "\n" + "Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All videos 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_video, "GET", params);
// Check your log cat for JSON reponse
Log.d("All videos: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// videos found
// Getting Array of videos
videos = json.getJSONArray(TAG_NAME);
// looping through All videos
for (int i = 0; i < videos.length(); i++) {
JSONObject c = videos.getJSONObject(i);
// Storing each json item in variable
String post = c.getString(TAG_POSITION);
String name = c.getString(TAG_MEMBER);
//videosList.add(c.getString(TAG_COMPANY));
//Log.d("item Name:", name);
//Log.d("companyNAme", videosList.get(0));
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_POSITION, post);
map.put(TAG_MEMBER, name);
// adding HashList to ArrayList
videosList.add(map);
//System.out.println(map);
}
}
else {
}
} 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 videos
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
// ListView Adapter
lv = (ListView) findViewById(R.id.lst_name);
lv.setAdapter(simpleAdapter);
}
});
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.btn_refresh)
{
videosList.clear();
new LoadAllVideos().execute();
}
else if(v.getId() == R.id.btn_home)
{
Intent intent = new Intent();
intent.setClassName("com.scba", "com.scba.Menu");
finish();
}
}
}
Screenshot:
just change your itemxml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/grayimage"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="president"
android:id="#+id/headerText"
/>
</FrameLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Prof. Dr. Khanuka"
android:id="#+id/DiscriptionText"
/>
</LinearLayout>
And then create Your Custem List from Here custom list
and edit your code according to you
Thanks
Related
I have list of cities in listview and now i want to add searchview in listview to search the item by specific letter like when i enter D in search view then all the cities should show with D in listview can anyone help me how can i do this i am new to android plaese help me.
this is my code in in this code only is showing in listview now i want to add searchview item please help
public class CityNameActivity extends ListActivity {
ListView list;
EditText inputSearch;
private ProgressDialog pDialog;
// URL to get Cities JSON
private static String url = "http://14.140.200.186/Hospital/get_city.php";
// JSON Node names
private static final String TAG_CITIES = "Cities";
private static final String TAG_ID = "city_id";
private static final String TAG_NAME = "city_name";
// Cities JSONArray
JSONArray Cities = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> citylist;
//ArrayList<String> citylist;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cityname_activity_main);
ListView listView=getListView();
inputSearch = (EditText) findViewById(R.id.inputSearch);
citylist = new ArrayList<HashMap<String, String>>();
// list.setOnClickListener(this);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String cityId= citylist.get(position).get(TAG_ID);
Intent in = new Intent(getApplicationContext(),
HospitalList_Activity.class);
in.putExtra("city_id",cityId);
startActivity(in);}
});
new GetCities().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetCities extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(CityNameActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
Cities = jsonObj.getJSONArray(TAG_CITIES);
// looping through All Cities
for (int i = 0; i < Cities.length(); i++) {
JSONObject c = Cities.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
HashMap<String, String> Cities = new HashMap<String, String>();
Cities.put(TAG_ID, id);
Cities.put(TAG_NAME, name);
// adding contact to Cities list
citylist.add(Cities);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**`enter code here`
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(CityNameActivity.this, citylist, R.layout.city_list_item, new String[] { TAG_NAME}, new int[] { R.id.name});
setListAdapter(adapter);
}
}}
cityname_activity_main.xml
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
city_list_item.xml
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textSize="16sp"
android:textStyle="bold" />
I have a class which extends listactivity. I am parsing data from a json file and putting data in list. i have also created a checkbox to check student is present if it checked. For that i have created an array to get the position from list. I am getting the student names but checkbox is giving me null values. Please Help!!! Here is my code,
public class Student_data extends ListActivity {
private ProgressDialog pDialog;
private Button button;
ListView lv;
// URL to get contacts JSON
private static String url = "http://10.0.2.2/studentdata.json";
private static int[] present, absent;
private static int i = 0, j = 0, x;
// JSON Node names
private static final String TAG_DEP_ID = "dep_id";
private static final String TAG_HEAD = "stdata";
private static final String TAG_NAME = "name";
private static final String TAG_ENROLL = "enrollment";
// contacts JSONArray
JSONArray data = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> studentList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_list);
studentList = new ArrayList<HashMap<String, String>>();
button = (Button) findViewById(R.id.saveData);
button.setOnClickListener(new ButtonClick());
lv = getListView();
new GetContacts().execute();
lv.setOnItemClickListener(new CheckBoxClick());
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Student_data.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
data = jsonObj.getJSONArray(TAG_HEAD);
// looping through All Contacts
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String id = c.getString(TAG_DEP_ID);
String name = c.getString(TAG_NAME);
String enrll = c.getString(TAG_ENROLL);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_DEP_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_ENROLL, enrll);
// adding contact to contact list
studentList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
*/
ListAdapter adapter = new SimpleAdapter(Student_data.this, studentList, R.layout.data_item,
new String[] { TAG_NAME }, new int[] { R.id.name });
setListAdapter(adapter);
}
}
public class CheckBoxClick implements OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
CheckBox chkbox = (CheckBox) arg1;
if (chkbox.isChecked()) {
present[i] = arg2;
i++;
}
if (chkbox.isChecked()) {
absent[j] = arg2;
j++;
}
}
}
public class ButtonClick implements OnClickListener {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
}
}
data_item.xml--->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:weightSum="2" >
<!-- Name Label -->
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#000"
android:textSize="16sp"
android:textStyle="bold" />
<CheckBox
android:id="#+id/chkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Try this
if (((CheckBox) arg1).isChecked()) {
//your code
}
I tried to make a simple application that will take the API from the internet, rearrange them and show as list.
In the emulator it works. The phone shows a white screen. The phone has Android 2.3.7. But when I tried it on the tablet, the app stopped working. The tablet has Android 4.1.
MainActivity
public class MainActivity extends ListActivity implements OnItemClickListener{
private ProgressDialog pDialog;
private String url = "http://www.cscpro.org/secura/market/"; //food-29-5.json;
private String res;
private String ql;
JSONArray market;
final static String RESOURCES = "res";
final static String QUALITY = "q";
private static final String TAG_OFFER = "offer";
private static final String TAG_PRICE = "price";
private static final String TAG_SALLER = "seller";
private static final String TAG_SALLER_NAME = "name";
ArrayList<HashMap<String, String>> listSaller;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listSaller = new ArrayList<HashMap<String, String>>();
Intent in = getIntent();
res = in.getStringExtra(RESOURCES);
ql = in.getStringExtra(QUALITY);
ListView lv = getListView();
lv.setOnItemClickListener(this);
// Calling async task to get json
new GetContacts().execute();
}
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String rate = ((TextView) view.findViewById(R.id.rate)).getText().toString();
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
// Starting single contact activity
Intent in = new Intent(getApplicationContext(),
SingleContactActivity.class);
in.putExtra(TAG_PRICE, rate);
in.putExtra(TAG_SALLER_NAME, name);
startActivity(in);
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String myURL = url+res+"-29-"+ql+".json";
String jsonStr = sh.makeServiceCall(myURL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
market = jsonObj.getJSONArray(TAG_OFFER);
// looping through All Contacts
for (int i = 0; i < market.length(); i++) {
JSONObject c = market.getJSONObject(i);
String rate = c.getString(TAG_PRICE);
// Phone node is JSON Object
JSONObject saller = c.getJSONObject(TAG_SALLER);
String name = saller.getString(TAG_SALLER_NAME);
// tmp hashmap for single contact
HashMap<String, String> sallers = new HashMap<String, String>();
// adding each child node to HashMap key => value
sallers.put(TAG_PRICE, rate);
sallers.put(TAG_SALLER_NAME, name);
// adding contact to contact list
listSaller.add(sallers);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, listSaller,
R.layout.list_item, new String[] { TAG_PRICE, TAG_SALLER_NAME}, new int[] { R.id.rate,
R.id.name});
setListAdapter(adapter);
}
}
}
SingleContactActivity
public class SingleContactActivity extends Activity {
// JSON node keys
private static final String TAG_PRICE = "price";
private static final String TAG_SALLER_NAME = "name";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_contact);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String rate = in.getStringExtra(TAG_PRICE);
String name = in.getStringExtra(TAG_SALLER_NAME);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblRate = (TextView) findViewById(R.id.rate_label);
lblName.setText(name);
lblRate.setText(rate);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
activity_single_contact.xml is a simple LinearLayout with two TextViews.
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 a JSON string which is generated by a php file. The problem is that the JSON string is not appearing on the textview of android. can anyone explain why?
public class AllUsersActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> usersList;
// url to get all users list
private static String url_all_users = "http://10.0.2.2/android_connect/get_all_users.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_USERS = "users";
private static final String TAG_UID = "UserID";
private static final String TAG_FIRSTNAME = "FirstName";
// users JSONArray
JSONArray users = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_users);
// Hashmap for ListView
usersList = new ArrayList<HashMap<String, String>>();
// Loading users in Background Thread
new LoadAllusers().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String uid = ((TextView) view.findViewById(R.id.uid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
UserDetailsActivity.class);
// sending uid to next activity
in.putExtra(TAG_UID, uid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllusers extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllUsersActivity.this);
pDialog.setMessage("Loading users. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All users 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_users, "GET", params);
// Check your log cat for JSON reponse
Log.d("All users: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// users found
// Getting Array of users
users = json.getJSONArray(TAG_USERS);
// looping through All users
for (int i = 0; i < users.length(); i++) {
JSONObject c = users.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_UID);
String name = c.getString(TAG_FIRSTNAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_UID, id);
map.put(TAG_FIRSTNAME, name);
// adding HashList to ArrayList
usersList.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 users
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllUsersActivity.this, usersList,
R.layout.list_item, new String[] { TAG_UID,
TAG_FIRSTNAME},
new int[] { R.id.uid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
this is the JSON string from the PHP file
{"Users":[{"0":[],"UserID":"1","FirstName":"lalawee","Email":"12345","Password":null},{"0":[],"UserID":"2","FirstName":"shadowblade721","Email":"12345","Password":null,"1":[]},{"0":[],"UserID":"3","FirstName":"dingdang","Email":"12345","Password":null,"1":[],"2":[]},{"0":[],"UserID":"4","FirstName":"solidsnake0328","Email":"12345","Password":null,"1":[],"2":[],"3":[]}],"success":1}
I've found the error.
It is caused by case sensitiveness.
users --> Users
Mistake on my part. Sorry.
Thanks for trying to help me!