vendredi 25 avril 2014

Adapter.clear removes values of of list attribute


Vote count:

0




I'm trying to set the values of a spinner depending on the selected item of the parent spinner. But leagueAdapter.clear(); emptys my attribute leagueValues (passed as a List to my SpinnerAdapter as the 3rd parameter) somehow. I only want to remove all current values in the spinner and set new ones. But calling clear() removes all values of leagueValues so my for-loop in getLeaguesByCountry returns an empty list.


Shortened code:



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



}


asked 2 mins ago

Chris

1,232





Aucun commentaire:

Enregistrer un commentaire