在另一个类中使用JSONArray?

我有一个微调,加载下拉列表中的客户的名字。

微调器从JSON数组中获取string。 我还收到了一些文字视图,其中选定的客户的姓名,地址,电话号码应该在select者改变时加载。

但是JSONArray被用在另一个类中,我怎样才能在另一个类中使用JSONArray呢?(如何在select改变时加载正确的客户细节?)

这我的代码:

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的样子:

 { "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个“客户”,因为它的虚拟数据)

Solutions Collecting From Web of "在另一个类中使用JSONArray?"

您可以将JsonArray转换为string,如下所示:

 String jsonString = jsonArray.toString(); 

将其保存为共享首选项:

  SharedPreferences settings = getSharedPreferences( "pref", 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("jsonString", jsonString); editor.commit(); 

然后在其他课上访问它。

 SharedPreferences settings = getSharedPreferences( "pref", 0); String jsonString= settings .getString("jsonString", null); 

一旦你获得了string,将其转换回JsonArray:

 JsonArray jsonArray = new JsonArray(jsonString); 

如果你想跨越不同的组件使用,另一个select是使用Parcelable接口。 以下是一个带有元素名称和job_title的Pojo类,它作为一个对象,可以使用接口Parcelable

 public class ContactPojo implements Parcelable{ private String name; private String job_title; public void setName(String name) { this.name = name; } public void setJob_title(String job_title) { this.job_title = job_title; } public String getName() { return name; } public String getJob_title() { return job_title; } private ContactPojo(Parcel parcel){ name=parcel.readString(); job_title=parcel.readString(); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int flags) { parcel.writeString(name); parcel.writeString(job_title); } public static final Parcelable.Creator<ContactPojo> CREATOR = new Parcelable.Creator<ContactPojo>() { public ContactPojo createFromParcel(Parcel in) { return new ContactPojo(in); } public ContactPojo[] newArray(int size) { return new ContactPojo[size]; }}; } 

您可以通过执行以下操作来填充pojo类

 ContactPojo contactPojo= new ContactPojo(); contactPojo.setName("name"); contactPojo.setJob_title("name"); 

并发送到ext意图由此

 Intent intent=new Intent(this, DetailView.class); intent.putExtra("Data", contactPojo); 

在接下来的步骤中检索下一个意图的数据

 ContactPojo contactPojo=new ContactPojo(); contactPojo=getIntent().getParcelableExtra("Data"); Log.i(AppConstants.APPUILOG, "Name: " + contactPojo.getName() ); 

你可以将你的json保存在一个文件中,然后可以在另一个类或者类似的地方获取它:

类来处理数据的保存和提取:

 public class RetriveandSaveJSONdatafromfile { public static String objectToFile(Object object) throws IOException { String path = Environment.getExternalStorageDirectory() + File.separator + "/AppName/App_cache" + File.separator; File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } path += "data"; File data = new File(path); if (!data.createNewFile()) { data.delete(); data.createNewFile(); } ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(data)); objectOutputStream.writeObject(object); objectOutputStream.close(); return path; } public static Object objectFromFile(String path) throws IOException, ClassNotFoundException { Object object = null; File data = new File(path); if(data.exists()) { ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(data)); object = objectInputStream.readObject(); objectInputStream.close(); } return object; } } 

要保存json文件,使用RetriveandSaveJSONdatafromfile.objectToFile(obj)并从文件中获取数据

  path = Environment.getExternalStorageDirectory() + File.separator + "/AppName/App_cache/data" + File.separator; RetriveandSaveJSONdatafromfile.objectFromFile(path); 

1)你可以得到你的mainactivity中的另一个类的input,并把json数据作为string响应

2)使用广播监听器和服务。 在服务中写入你的json响应,并使用广播意图将其发送回mainactivity。 主要活动中的广播接收器可以收听具有json数据的服务。 同时更新textviews。