我正在寫一個應用程序,我正在使用JSON將數據提取到ListView中,並且我已經爲第一級(eventName)獲取數據,但在獲取第二級(angelName)時獲取空白活動...使用JSON獲取數據
JSON: -
[
{
"eventName" : "Honda Motors",
"angelList" : [
{
"angelID": "1",
"angelName": "Angel A"
},
{
"angelID": "2",
"angelName": "Angel B"
}
]
}
]
總之,無法得到AngelName,
AngelsActivity.java:-
public class AngelsActivity extends ListActivity {
// Connection detector
ConnectionDetector cd;
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> angelsList;
// albums JSONArray
JSONArray arrayAngels = null;
// Category name
String category_Name;
// ALL JSON node names
private static final String ANGEL_NAME = "angelName";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
cd = new ConnectionDetector(getApplicationContext());
// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(AngelsActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Hashmap for ListView
angelsList = new ArrayList<HashMap<String, String>>();
// Loading Albums JSON in Background Thread
new LoadAlbums().execute();
// get listview
ListView lv = getListView();
/**
* Listview item click listener
* TrackListActivity will be lauched by passing album id
* */
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
}
});
}
/**
* Background Async Task to Load all Albums by making http request
* */
class LoadAlbums extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AngelsActivity.this);
pDialog.setMessage("Listing Angels ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Categories JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(EventsActivity.URL_CATEGORIES, "GET",
params);
try {
arrayAngels = new JSONArray(json);
if (arrayAngels != null) {
// looping through All albums
for (int i = 0; i < arrayAngels.length(); i++) {
JSONObject c = arrayAngels.getJSONObject(i);
// Storing each json item values in variable
String name = c.getString(ANGEL_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(ANGEL_NAME, name);
// adding HashList to ArrayList
angelsList.add(map);
}
}else{
Log.d("Angels: ", "null");
}
} 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 albums
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AngelsActivity.this, angelsList,
R.layout.list_item,
new String[] {ANGEL_NAME},
new int[] {R.id.category_name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
做的感謝...非常coolll ...非常需要和太容易 – Sun
@Abrahim Neil歡迎 – SathishKumar
獲得所有事件的第一個事件的天使名稱,如EventA(AngelA,AngelB),EventB(AngelC,AngelD)。所以我得到AngelA&AngelB,或者我點擊EventA或EventB,爲什麼? – Sun