Lutaaya Huzaifah Lutaaya Huzaifah - 1 month ago 7
Android Question

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

This is the Array of the two spinners + their declarations in the

onCreate
am using.

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);


I have populated the two spinners with that data above, then i sent all the selected items into the URL using this format , the
regTester
is got from the previous
Activity


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

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


below is my 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();


This is my Full method , am using ION library in loading my JSON data below , the data loads but when I try to change both the spinners items, the data doesn't reload. How can I make it reload whenever I change the items in the spinner.

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();
}
});




}

Answer

Try editing your Load(), to below;

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();
                            }
                        }
                    });




    }

Then go in the onStart() or onCreate and do the following;

 @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();

    }
Comments