简体   繁体   中英

Refresh items in ExpandableListView

I have an Activity that contains an ExpandableListView. In certain cases, the user has the ability to add items to one of the expandable lists. When they do so however, I cannot get the list to refresh to reflect their change. If I back out of the activity and go back into it, then I can see the change. Here is the code from the Activity, relevant methods only. Let me know if I should add any additional code.

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

Some functions omitted for conciseness

    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();

    }

I think your problem is that you are not adding that newly created item to the adapter's collection. The reason why your call to expAdapter.notifyDataSetChanged() does not seem to work is because there is actually no change to the data set. You added the new item to the database, but you did not update the adapter's list. So as far as the adapter is concerned there was no update to the item list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM