我正在尝试根据父微调器的选定项设置微调器的值。但leagueAdapter.clear();
以某种方式将我的属性leagueValues
(作为List
传递给我的SpinnerAdapter
作为第三个参数)。
我只想删除微调器中的所有当前值并设置新值。但是调用clear()
会删除leagueValues
的所有值,因此getLeaguesByCountry
中的for循环会返回一个空列表。
缩短代码:
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 :(得分: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
能够恢复它们。