刷新ExpandableListView中的项目

时间:2013-06-12 05:19:38

标签: android expandablelistview

我有一个包含ExpandableListView的Activity。在某些情况下,用户可以将项目添加到其中一个可扩展列表中。然而,当他们这样做时,我无法让列表刷新以反映他们的变化。如果我退出活动并回到它,那么我可以看到变化。以下是Activity的代码,仅与相关方法有关。如果我应该添加任何其他代码,请告诉我。

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

    }

1 个答案:

答案 0 :(得分:1)

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