I have got a spinner that loads the name of the customers in the dropdown.
The spinner gets the string from a JSON array. I have also got a few textviews where the Name,Adress,Telephone number of the selected customer should load when the spinners selection changes.
But the JSONArray is used in another class, how can I use the JSONArray in another class?(How can I load the correct customer details when spinner selection changes?)
This my code:
public class Gegevens extends Main {
Spinner spCustomers;
private JSONObject jsonChildNode;
private JSONArray jsonMainNode;
private String name;
private TextView txtNaam;
private TextView txtAdres;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gegevens);
new AsyncLoadCustDetails().execute();
spCustomers = (Spinner) findViewById(R.id.spKlanten);
spCustomers.setOnItemSelectedListener(new mySelectedListener());
txtNaam = (TextView)findViewById(R.id.txtNaam);
}
protected class AsyncLoadCustDetails extends
AsyncTask<Void, JSONObject, ArrayList<String>> {
ArrayList<CustomerDetailsTable> custTable = null;
@Override
protected ArrayList<String> doInBackground(Void... params) {
RestAPI api = new RestAPI();
ArrayList<String> spinnerArray = null;
try {
JSONObject jsonObj = api.GetCustomerDetails();
JSONParser parser = new JSONParser();
custTable = parser.parseCustomerDetails(jsonObj);
spinnerArray = new ArrayList<String>();
//All i can think of is make new array for each value?
Log.d("Customers: ", jsonObj.toString());
jsonMainNode = jsonObj.optJSONArray("Value");
for (int i = 0; i < jsonMainNode.length(); i++) {
jsonChildNode = jsonMainNode.getJSONObject(i);
name = jsonChildNode.optString("Naam");
spinnerArray.add(name);
}
} catch (Exception e) {
Log.d("AsyncLoadCustDetails", e.getMessage());
}
return spinnerArray;
}
@Override
protected void onPostExecute(ArrayList<String> spinnerArray) {
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item); // The drop down view
spCustomers.setAdapter(spinnerArrayAdapter);
}
}
public class mySelectedListener implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
String value = (String) parent.getItemAtPosition(pos);
txtNaam.setText(value); //got the name working since it wasnt that hard
//load the other details in the textviews
}
@Override
public void onNothingSelected(AdapterView parent) {
}
}
}
This is what the jsonObj looks like:
{
"Successful": true,
"Value": [
{
"Naam": "Google",
"Adres": "Kerkstraat 3",
"Postcode": "4455 AK Roosendaal",
"Telefoon": "0165-559234",
"Email": "[email protected]",
"Website": "www.google.nl"
},
{
"Naam": "Apple",
"Adres": "Kerkstraat 4",
"Postcode": "4455 AD Roosendaal",
"Telefoon": "0164-559234",
"Email": "[email protected]",
"Website": "www.apple.nl"
}
]
}
(Only 2 "customers", since its dummy data)
spinnerArrayto other activity?