使用JSONArray另一个类? [英] Use JSONArray in another class?
问题描述
我有,在下拉列表中加载客户的名称微调。
I have got a spinner that loads the name of the customers in the dropdown.
微调器会从一个JSON数组的字符串。
我也得到了一些textviews,其中所选客户的名称,ADRESS,电话号码应该加载纺纱厂选择更改时。
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.
但JSONArray在另一个类使用,我该如何使用JSONArray在另一个类?(我怎样才能加载正确的客户详细信息时微调选择更改?)
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?)
这是我的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) {
}
}
}
这是jsonObj的样子:
This is what the jsonObj looks like:
{
"Successful": true,
"Value": [
{
"Naam": "Google",
"Adres": "Kerkstraat 3",
"Postcode": "4455 AK Roosendaal",
"Telefoon": "0165-559234",
"Email": "info@google.nl",
"Website": "www.google.nl"
},
{
"Naam": "Apple",
"Adres": "Kerkstraat 4",
"Postcode": "4455 AD Roosendaal",
"Telefoon": "0164-559234",
"Email": "info@apple.nl",
"Website": "www.apple.nl"
}
]
}
(仅2顾客,因为它的虚拟数据)
(Only 2 "customers", since its dummy data)
推荐答案
您可以将JsonArray转换为字符串如下:
You can convert the JsonArray to string as follow :
String jsonString = jsonArray.toString();
其保存在共享preference:
save it in shared preference :
SharedPreferences settings = getSharedPreferences(
"pref", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("jsonString", jsonString);
editor.commit();
然后访问它在其他类。
And then access it in other class.
SharedPreferences settings = getSharedPreferences(
"pref", 0);
String jsonString= settings
.getString("jsonString", null);
一旦获得字符串,将其转换回JsonArray:
Once you have obtained the String, convert it back to JsonArray :
JsonArray jsonArray = new JsonArray(jsonString);
这篇关于使用JSONArray另一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!