在列表中仅获取第一个事件的天使名称,要么点击第二个或第三个事件或任何其他事件..
我希望每当用户点击事件本田汽车需要显示天使A& B,每当用户点击Event Tata Motors需要显示Angel C& D ....我在我的json和1000多个天使中有大约200个事件,有些事件有50个或者有一些有200个天使,但我需要向天使展示仅用于事件......
JSON: -
[
{
"eventName" : "Honda Motors",
"angelList" : [
{
"angelID": "1",
"angelName": "Angel A"
},
{
"angelID": "2",
"angelName": "Angel B"
}
]
},
{
"eventName" : "Tata Motors",
"angelList" : [
{
"angelID": "1",
"angelName": "Angel C"
},
{
"angelID": "2",
"angelName": "Angel D"
}
]
}
]
举一个例子,第一个EventName是本田汽车,有两个天使天使A,B和第二个EventName是塔塔汽车,这个事件还有两个天使,即天使C,天使D ....我的问题是每当我做点击Tata Motors将Angel A,Angel B列入名单,而不是展示Angel C,Angel D ......
代码: -
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> angelsList;
// angels JSONArray
JSONArray arrayAngels = null;
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(EventsActivity.URL_JSON, "GET", params);
JSONObject jsonData;
try {
jsonData = new JSONArray(json).getJSONObject(0);
arrayAngels = jsonData.optJSONArray("angelList");
if (arrayAngels != null) {
// looping through all angels
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);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
完整代码: -
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;
// angels JSONArray
JSONArray arrayAngels = null;
String intentEvents = null;
public static final String LOG_TAG = "AngelsActivity";
// JSON node names
private static final String ANGEL_NAME = "angelName";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
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;
}
// Get EventName using Intent (from EventsActivity)
Intent i = getIntent();
intentEvents = (i.getStringExtra("event_name"));
// Hashmap for ListView
angelsList = new ArrayList<HashMap<String, String>>();
// Loading Angels JSON in Background Thread
new LoadAngels().execute();
// get listview
ListView lv = getListView();
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 Angels by making http request
* */
class LoadAngels 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("Loading Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(EventsActivity.URL_JSON, "GET", params);
JSONArray jsonData;
try {
jsonData = new JSONArray(json);
for (int j = 0; j < jsonData.length(); j++) {
arrayAngels = jsonData.getJSONObject(j).optJSONArray("angelList");
if (arrayAngels != null) {
// looping through all angels
for (int i = 0; i < arrayAngels.length(); i++) {
Log.d(AngelsActivity.LOG_TAG, "arrayAngels.length " + arrayAngels.length());
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);
}
}
}
} 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 angels
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.name });
// updating listview
setListAdapter(adapter);
// Change activity Title with Event Name
setTitle(intentEvents);
}
});
}
}
}
答案 0 :(得分:0)
在适配器中,进行更改
jsonData = new JSONArray(json).getJSONObject(0);
的{{1}}