Использование getEvents (внизу) Я могу показать список событий.Android Parse JSON
{
"events": [
{
"id": "1",
"name": "Winter Festival",
"multiOperatives": "1,3"
},
{
"id": "2",
"name": "Summer Festival",
"multiOperatives": "2,4"
}
]
}
JSON для синтаксического анализа оперативников getOperatives
{
"operatives": [
{
"id": "1",
"firstName": "Operative 1"
},
{
"id": "2",
"firstName": "Operative 2"
},
{
"id": "3",
"firstName": "Operative 3"
},
{
"id": "4",
"firstName": "Operative 4"
}
]
}
, но теперь я не знаю как показать конкретные Оперативник только, как я хочу каждый раз, когда пользователь действительно нажмите на летнем фестивале, тогда мне просто нужно показать только два оперативных органа а именно, Оперативно 2 и 4 постановляющей ....
GetEventsActivity.java:
public class GetEventsActivity extends ListActivity {
// url to make request
private static String url = "http://myurl.tld/webservice.php?ws=getEvents&u=p&p=y";
// JSON Node names
private static final String TAG_EVENTS = "events";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_MULTI_OPERATIVES = "multiOperatives";
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Connection detector
ConnectionDetector cd;
// events JSONArray
JSONArray events = null;
String multiOperatives;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events);
cd = new ConnectionDetector(getApplicationContext());
// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(GetEventsActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of events
events = json.getJSONArray(TAG_EVENTS);
// looping through All events
for(int i = 0; i < events.length(); i++){
JSONObject c = events.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
multiOperatives = c.getString(TAG_MULTI_OPERATIVES);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_MULTI_OPERATIVES, multiOperatives);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.activity_list,
new String[] { TAG_NAME }, new int[] {
R.id.name });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(GetEventsActivity.this, GetOperativesActivity.class);
intent.putExtra("multiOperatives", multiOperatives);
startActivity(intent);
}
});
}
}
GetOperativesActivity.java:-
public class GetOperativesActivity extends ListActivity {
// url to make request
private static String url = "http://myurl.tld/webservice.php?ws=getOperatives&u=p&p=y";
// JSON Node names
private static final String TAG_OPERATIVES = "operatives";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "firstName";
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Connection detector
ConnectionDetector cd;
// events JSONArray
JSONArray events = null;
String multiOperatives;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_operatives);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
cd = new ConnectionDetector(getApplicationContext());
// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(GetOperativesActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
Intent intent = getIntent();
multiOperatives = intent.getStringExtra("multiOperatives");
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of events
events = json.getJSONArray(TAG_OPERATIVES);
// looping through All events
for(int i = 0; i < events.length(); i++){
JSONObject c = events.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.activity_list,
new String[] { TAG_NAME }, new int[] {
R.id.name });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
Используя выше код я состоянии для получения списка событий и операций, но не знаю, как показывать только требуемые операторы для п Event ..... не все Оперативники ... как я хочу каждый раз, когда пользователь этого нажмите на летнем фестивале, то я просто нужно показать только два оперативник именно Оперативно 2 и 4 постановляющей ....
Вам просто нужно для отображения 'multiOperatives' с вашим идентификатором' operatives '. – SilentKiller
Эй, @ СиленКиллер, привет, но запутавшись, не могли бы вы показать мне дорогу? – Sun