ExpandableListView を含むアクティビティがあります。場合によっては、ユーザーは展開可能なリストの 1 つに項目を追加できます。ただし、そうすると、リストを更新して変更を反映させることができません。アクティビティから戻って戻ると、変化が見られます。これはアクティビティのコードで、関連するメソッドのみです。コードを追加する必要がある場合はお知らせください。
public class Settings extends Activity {
private ExpandListAdapter expAdapter;
private ArrayList<ExpandListGroup> expListGroups;
private ExpandableListView expandableList;
private String client;
private EventsDB db;
private ArrayList<ClientFinderCategories> categoriesList = null;
private ArrayList<ClientFinderLocations> locationsList = null;
private View builderView;
private AlertDialog alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smart_settings);
//expandableList = getExpandableListView();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
client = sharedPref.getString("client", "none");
db = new EventsDB(this);
/* Establish the two groups */
expListGroups = new ArrayList<ExpandListGroup>();
setGroups();
/* Set up the data adapter */
expAdapter = new ExpandListAdapter(SmartSettings.this, expListGroups);
populateExpandableGroups();
}
private void populateExpandableGroups() {
expandableList = (ExpandableListView) findViewById(R.id.expandable_list);
expandableList.setAdapter(expAdapter);
expandableList.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0) {
categoryClickHandler(parent, childPosition);
} else if (groupPosition == 1) {
locationClickHandler(parent, childPosition);
}
feedback();
return true;
}
});
expAdapter.notifyDataSetChanged();
}
簡潔にするために一部の関数は省略されています
private void locationClickHandler(ExpandableListView parent, int childPosition) {
String city = locationsList.get(childPosition).getCity();
String state = locationsList.get(childPosition).getState();
Boolean isSelected = !locationsList.get(childPosition).getIsSelected();
/* Handle the case where the user has selected 'Add Location' */
if (city.equals("Add Location")) {
addNewLocation();
}
}
private void addNewLocation() {
LayoutInflater inflater = LayoutInflater.from(this);
builderView = inflater.inflate(R.layout.city_state_entry, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(builderView);
alert = builder.create();
alert.show();
}
//This function is declared in the XML file for the 'Submit' button
public void submitCityStateButtonOnClick(View view) {
EditText city_entry = (EditText) builderView.findViewById(R.id.city_entry);
Spinner state_entry = (Spinner) builderView.findViewById(R.id.state_spinner);
String city = city_entry.getText().toString();
String state = state_entry.getSelectedItem().toString();
alert.dismiss();
db.setClientLocation(client, city, state, true);
locationsList = db.getLocationsForClient(client);
//This is where I am trying to refresh the expandable list
//you can see my various attempts in the commented lines
System.err.println("refreshing expandable list");
//expAdapter = new ExpandListAdapter(SmartSettings.this, expListGroups);
expAdapter.notifyDataSetInvalidated();
//expAdapter.notifyDataSetChanged();
}