2014-04-25 61 views
2

我試圖根據父微調器的選定項目設置微調器的值。但leagueAdapter.clear();以某種方式清空了我的屬性leagueValues(作爲List傳遞給我的SpinnerAdapter作爲第三個參數)。 我只想刪除微調器中的所有當前值並設置新值。但撥打clear()刪除leagueValues的所有值,所以我的for循環getLeaguesByCountry返回一個空列表。Adapter.clear刪除列表屬性的值

縮短代碼:

public class AddBetActivity extends MainActivity implements OnItemSelectedListener { 
    // spinners 
    Spinner countrySpinner; 
    // ... 

    // values 
    List<DataObject> countryValues = new ArrayList<DataObject>(); 
    List<DataObject> leagueValues = new ArrayList<DataObject>(); 
    // ... 

    // adapters 
    SpinnerAdapter countryAdapter; 
    // ... 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_add_bet); 

     // spinners 
     countrySpinner = (Spinner) findViewById(R.id.country); 
     // ... 


     // load data 
     loadData(); 

     // set listener for button onClick 
     findViewById(R.id.submit).setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         validate(); 
        } 
       }); 
    } 

    protected void setListener() { 
     countrySpinner.setOnItemSelectedListener(this); 
     // ... 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
     switch(parent.getId()) { 
      case R.id.country: 
       DataObject country = countryAdapter.getItem(pos); 
       if (country.getID() != 0) { 
        // get new values 
        ArrayList<League> list = getLeaguesByCountry(country.getID()); 

        // clear old values 
        leagueAdapter.clear(); 

        // add new values 
        leagueAdapter.addAll(list); 

        // notify adapter 
        leagueAdapter.notifyDataSetChanged(); 
        leagueSpinner.setVisibility(View.VISIBLE); 
       } 
       else { 
        leagueSpinner.setVisibility(View.INVISIBLE); 
       } 
      break; 

      // ... 
     } 
    } 


    public void onNothingSelected(AdapterView<?> arg0) { 
     // TO-DO 
    } 

    public void loadData() { 
     DataUtil.post("GetBetData", params, new JsonHttpResponseHandler() { 
      public void onSuccess(int statusCode, Header[] headers, JSONObject response) { 
       try { 
        // get arrays 
        JSONArray countries = response.getJSONArray("countries"); 
        // ... 


        // add default value 
        countryValues.add(new DataObject(0, getString(R.string.default_country))); 
        // add countries 
        for(int i=0; i<countries.length(); i++) { 
         JSONObject country = countries.getJSONObject(i); 
         countryValues.add(new DataObject(country.getInt("countryID"), country.getString("name"))); 
        } 

        // ... 
       } 
       catch (JSONException e) { 
        Log.e("ERROR", e.getMessage()); 
       } 

       countryAdapter = new SpinnerAdapter(getApplicationContext(), R.layout.spinner_style, countryValues); 
       countrySpinner.setAdapter(countryAdapter); 

       leagueAdapter = new SpinnerAdapter(getApplicationContext(), R.layout.spinner_style, leagueValues); 
       leagueSpinner.setAdapter(leagueAdapter); 

       // ... 

       // set on select listener 
       setListener(); 
      } 
     });  
    } 


    public void validate() {   
     // TO-DO 
    } 

    public ArrayList<League> getLeaguesByCountry(int countryID) { 
     ArrayList<League> list = new ArrayList<League>(); 

     for(League league: leagueValues) { 
      if (league.getCountry() == countryID) 
       list.add(league); 
     } 
     return list; 
    } 



} 

編輯: 我嘗試添加另一個屬性List<League> tempLeagueValues = new ArrayList<League>();我增加值在我loadData方法

 tempLeagueValues = leagueValues; 

     countryAdapter = new SpinnerAdapter(getApplicationContext(), R.layout.spinner_style, countryValues); 
     countrySpinner.setAdapter(countryAdapter); 

     leagueAdapter = new SpinnerAdapter(getApplicationContext(), R.layout.spinner_style, leagueValues); 
     leagueSpinner.setAdapter(leagueAdapter); 

而且我還用它在我的getLeaguesByCountry方法:

public ArrayList<League> getLeaguesByCountry(int countryID) { 
    ArrayList<League> list = new ArrayList<League>(); 

    for(League league: tempLeagueValues) { 
     if (league.getCountry() == countryID) 
      list.add(league); 
    } 
    return list; 
} 

第二次在我的countrySpinner中選擇一個項目時,tempLeagueValues爲空。怎麼可能?

+0

當然SpinnerAdapter.clear()會清除它的值,但是我不清楚你想要什麼。 – shkschneider

+0

它不應該清除我作爲第三個參數傳遞給SpinnerAdapter的屬性,對吧?但它確實如此。我不通過一個參考,這就是爲什麼我想知道。 – Chris

+0

它的確如此。第三個參數是值,清除() – shkschneider

回答

2

clear()方法正在做它應該做的事情。

您需要保留一組可能的值以恢復。

public ArrayList<League> getLeaguesByCountry(int countryID) { 
    ArrayList<League> list = new ArrayList<League>(); 

    for(League league: savedLeagueValues) { 
     if (league.getCountry() == countryID) 
      list.add(league); 
    } 
    return list; 
} 

有了:

savedLeagueValues = new ArrayList<League>(leagueValues); 
// this is a java "trick" to directly copy values from an array to another 

爲了能夠恢復它們。