0

我有一个包含 ExpandableListView 的活动。在某些情况下,用户能够将项目添加到可扩展列表之一。但是,当他们这样做时,我无法刷新列表以反映他们的更改。如果我退出活动并重新开始,那么我可以看到变化。这是来自活动的代码,仅相关方法。让我知道是否应该添加任何其他代码。

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

    }
4

1 回答 1

2

我认为您的问题是您没有将新创建的项目添加到适配器的集合中。您的调用expAdapter.notifyDataSetChanged()似乎不起作用的原因是因为数据集实际上没有更改。您将新项目添加到数据库中,但没有更新适配器的列表。因此,就适配器而言,项目列表没有更新。

于 2015-12-22T02:26:36.200 回答