2016-10-28 67 views
0

這是兩個spinners的數組+他們在onCreate中使用的聲明。在更改微調器中的項目時將數據加載到Url中異步加載JSON

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

這是我的全部方法,現在用在加載我下面的JSON數據,數據加載ION庫,但是當我試圖同時改變紡紗項目,數據不會重新加載。每當我更改微調器中的項目時,如何重新加載它。

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




    } 
+0

你需要打電話給你的load()方法對微調的onItemSelected()方法,所以當你改變任何微調的值,它會調用 – Vickyexpert

+0

嘿Vickyexpert,感謝您的回覆,但我仍然困惑,我該怎麼當它們在'Load'方法內時,調用'Load'方法到每個spinners上。請告訴我我該怎麼做 –

回答

0

嘗試編輯您的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(); 

    } 
+0

是的,這工作正常,它只是一個簡單的邏輯 –