将数据加载到 Url 中,同时更改微调器中的项目异步加载 JSON

Loading data into a Url while changing an Item in a spinner Asynchronously to load JSON

这是两个微调器的数组 + 它们在 onCreate 中的声明。

 String semester_list [] = {"1","2","3"};
        String acad_yr  []= {"1","2","3","4","5","6"};
        semester = (Spinner)findViewById(R.id.spinner_semester);
        year_of_study=(Spinner)findViewById(R.id.spinner_year_of_study);

我已经用上面的数据填充了两个微调器,然后我使用这种格式将所有选定的项目发送到 URL,regTester 是从之前的 Activity

 inn = getIntent();
        regTester = inn.getStringExtra("RegNumber");

        sem = semester.getSelectedItem().toString();
        yr = year_of_study.getSelectedItem().toString();

下面是我的URL.

final String url = Uri.parse("http://10.0.2.2:84/COOPERP/Mobile/Default.aspx").buildUpon()
                    .appendQueryParameter("DataFormat", "StudentsResults")
                    .appendQueryParameter("reg", regTester)
                    .appendQueryParameter("StYr", yr)
                    .appendQueryParameter("sem", sem)
                    .build().toString();

这是我的完整方法,我在下面使用 ION 库加载我的 JSON 数据,数据加载但当我尝试更改两个微调器项目时,数据不会重新加载。每当我更改微调器中的项目时,如何让它重新加载。

public void Load(){
        inn = getIntent();
        regTester = inn.getStringExtra("RegNumber");
        sem = semester.getSelectedItem().toString();
        yr = year_of_study.getSelectedItem().toString();



            final String url = Uri.parse("http://10.0.2.2:84/COOPERP/Mobile/Default.aspx").buildUpon()
                    .appendQueryParameter("DataFormat", "StudentsResults")
                    .appendQueryParameter("reg", regTester)
                    .appendQueryParameter("StYr", yr)
                    .appendQueryParameter("sem", sem)
                    .build().toString();

            //final String url_ ="http://10.0.2.2:84/COOPERP/Mobile/Default.aspx?DataFormat=StudentsResults&reg="+regTester+"&StYr="+yr+"&sem="+sem;



        Toast.makeText(ResultsActivity.this,""+ url,Toast.LENGTH_LONG).show();


            final ProgressDialog pd;
            pd = new ProgressDialog(ResultsActivity.this);
            pd.setMessage("Loading....");
            pd.setCancelable(false);
            pd.show();


            Ion.with(getBaseContext())
                    .load(url)
                    .progressDialog(pd)
                    .as(new TypeToken<List<StudentsResults>>() {
                    })
                    .setCallback(new FutureCallback<List<StudentsResults>>() {

                        @Override
                        public void onCompleted(Exception e, List<StudentsResults> itemList) {
                            final StudentsResultsAdapter adapter = new StudentsResultsAdapter(itemList, getApplicationContext());
                            if (itemList != null) {

                                String Gradept = itemList.get(0).getGradept().toString();
                                String Gpa = itemList.get(0).getGpa().toString();
                                gpa.setText(Gpa);
                                gradept.setText(Gradept);
                                rv.setAdapter(adapter);
                                rv.hasFixedSize();
                                rv.setLayoutManager(new LinearLayoutManager(getBaseContext()));

                                //  rv.setLayoutManager(new LinearLayoutManager(getBaseContext()));

                            } else {
                                rv.setLayoutManager(new LinearLayoutManager(getBaseContext()));
                                Toast.makeText(getApplicationContext(), "No Results found!", Toast.LENGTH_SHORT).show();
                            }

                            pd.dismiss();
                        }
                    });




    }

尝试将您的 Load() 编辑到下方;

public void Load(String RegNumber, String Semester , String Year){



            final String url = Uri.parse("http://10.0.2.2:84/COOPERP/Mobile/Default.aspx").buildUpon()
                    .appendQueryParameter("DataFormat", "StudentsResults")
                    .appendQueryParameter("reg", RegNumber)
                    .appendQueryParameter("StYr", Year)
                    .appendQueryParameter("sem", Semester)
                    .build().toString();


            final ProgressDialog pd;
            pd = new ProgressDialog(ResultsActivity.this);
            pd.setMessage("Loading....");
            pd.setCancelable(false);
            pd.show();


            Ion.with(getBaseContext())
                    .load(url)
                    .progressDialog(pd)
                    .as(new TypeToken<List<StudentsResults>>() {
                    })
                    .setCallback(new FutureCallback<List<StudentsResults>>() {

                        @Override
                        public void onCompleted(Exception e, List<StudentsResults> itemList) {
                            final StudentsResultsAdapter adapter = new StudentsResultsAdapter(itemList, getApplicationContext());

                            try {

                                if (itemList != null) {

                                    String Gradept = itemList.get(0).getGradept().toString();
                                    String Gpa = itemList.get(0).getGpa().toString();
                                    gpa.setText(Gpa);
                                    gradept.setText(Gradept);
                                    rv.setAdapter(adapter);
                                    rv.hasFixedSize();
                                    rv.setLayoutManager(new LinearLayoutManager(getBaseContext()));

                                } else {
                                    gpa.setText(null);
                                    gradept.setText(null);
                                    rv.setAdapter(null);
                                    rv.setLayoutManager(new LinearLayoutManager(getBaseContext()));
                                    Toast.makeText(getApplicationContext(), "No Results found!", Toast.LENGTH_SHORT).show();
                                }

                                pd.dismiss();

                            }
                            catch (Exception ex){
                                Toast.makeText(getApplicationContext(), "No Results found!", Toast.LENGTH_SHORT).show();
                                pd.dismiss();
                            }
                        }
                    });




    }

然后进入 onStart()onCreate 并执行以下操作;

 @Override
    protected void onStart() {


        //GetStudentResults();


     semester.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        inn = getIntent();
                        regTester = inn.getStringExtra("RegNumber");
                        sem = semester.getSelectedItem().toString();
                        yr = year_of_study.getSelectedItem().toString();
                        Load(regTester, sem, yr);
                        break;
                    case 1:
                        inn = getIntent();
                        regTester = inn.getStringExtra("RegNumber");
                        sem = semester.getSelectedItem().toString();
                        yr = year_of_study.getSelectedItem().toString();
                        Load(regTester, sem, yr);
                        break;
                    case 2:
                        inn = getIntent();
                        regTester = inn.getStringExtra("RegNumber");
                        sem = semester.getSelectedItem().toString();
                        yr = year_of_study.getSelectedItem().toString();
                        Load(regTester, sem, yr);
                        break;

                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


      year_of_study.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        inn = getIntent();
                        regTester = inn.getStringExtra("RegNumber");
                        sem = semester.getSelectedItem().toString();
                        yr = year_of_study.getSelectedItem().toString();
                        Load(regTester, sem, yr);
                        break;
                    case 1:
                        inn = getIntent();
                        regTester = inn.getStringExtra("RegNumber");
                        sem = semester.getSelectedItem().toString();
                        yr = year_of_study.getSelectedItem().toString();
                        Load(regTester, sem, yr);
                        break;
                    case 2:
                        inn = getIntent();
                        regTester = inn.getStringExtra("RegNumber");
                        sem = semester.getSelectedItem().toString();
                        yr = year_of_study.getSelectedItem().toString();
                        Load(regTester, sem, yr);
                        break;
                    case 3:
                        inn = getIntent();
                        regTester = inn.getStringExtra("RegNumber");
                        sem = semester.getSelectedItem().toString();
                        yr = year_of_study.getSelectedItem().toString();
                        Load(regTester, sem, yr);
                        break;
                    case 4:
                        inn = getIntent();
                        regTester = inn.getStringExtra("RegNumber");
                        sem = semester.getSelectedItem().toString();
                        yr = year_of_study.getSelectedItem().toString();
                        Load(regTester, sem, yr);
                        break;
                    case 5:
                        inn = getIntent();
                        regTester = inn.getStringExtra("RegNumber");
                        sem = semester.getSelectedItem().toString();
                        yr = year_of_study.getSelectedItem().toString();
                        Load(regTester, sem, yr);
                        break;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });




        //load2();

        super.onStart();

    }