当前位置:  开发笔记 > 编程语言 > 正文

在另一个类中使用JSONArray?

如何解决《在另一个类中使用JSONArray?》经验,为你挑选了1个好方法。

我有一个微调器,可以在下拉列表中加载客户的名称.

微调器从JSON数组中获取字符串.我还有一些文本视图,其中当微调器选择改变时,所选客户的名称,地址,电话号码应该加载.

但是JSONArray在另一个类中使用,我如何在另一个类中使用JSONArray?(当微调器选择发生变化时,如何加载正确的客户详细信息?)

这是我的代码:

     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> {
                ArrayList custTable = null;

                @Override
                protected ArrayList doInBackground(Void... params) {

                    RestAPI api = new RestAPI();
                    ArrayList spinnerArray = null;
                    try {

                        JSONObject jsonObj = api.GetCustomerDetails();

                        JSONParser parser = new JSONParser();

                        custTable = parser.parseCustomerDetails(jsonObj);
                        spinnerArray = new ArrayList();
//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 spinnerArray) {
                    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(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个"客户",因为它的虚拟数据)



1> Sreehari..:

如果要跨不同组件使用,则另一个选项是使用Parcelable Interface.下面是一个POJO类元素的名称和JOB_TITLE已经作出作为可跨越意图使用接口来传递的对象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 CREATOR = new
            Parcelable.Creator() {
                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() );

推荐阅读
kikokikolove
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有