상위 스피너의 선택한 항목에 따라 회 전자 값을 설정하려고합니다. 그러나 leagueAdapter.clear();
emptys 내 속성 leagueValues
(제 3 매개 변수로 을 내 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;
}
}
편집 : 나는 내 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);
에 내가 값을 추가 다른 속성 List<League> tempLeagueValues = new ArrayList<League>();
를 추가하려고 그리고 나는 또한 내 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
은 비어 있습니다. 어떻게 그렇게 될수 있니?
물론 SpinnerAdapter.clear()는 값을 지우지 만 원하는대로 나에게 명확하지 않습니다. – shkschneider
SpinnerAdapter에 제 3의 매개 변수로 전달한 속성을 삭제해서는 안됩니다. 맞습니까? 하지만 그렇습니다. 나는 내가 왜 궁금해하는지 언급하지 않는다. – Chris
정확히 그 일을합니다. 세 번째 인수는 clear()로 제거되는 값입니다. – shkschneider