Android 刷新ExpandableListView中的项目
我有一个包含ExpandableListView的活动。在某些情况下,用户可以将项目添加到一个可扩展列表中。但是,当他们这样做时,我无法刷新列表以反映他们的更改。如果我退出活动并重新开始,那么我就能看到变化。以下是活动的代码,仅限相关方法。让我知道我是否应该添加任何附加代码Android 刷新ExpandableListView中的项目,android,expandablelistview,Android,Expandablelistview,我有一个包含ExpandableListView的活动。在某些情况下,用户可以将项目添加到一个可扩展列表中。但是,当他们这样做时,我无法刷新列表以反映他们的更改。如果我退出活动并重新开始,那么我就能看到变化。以下是活动的代码,仅限相关方法。让我知道我是否应该添加任何附加代码 public class Settings extends Activity { private ExpandListAdapter expAdapter; private ArrayList<Exp
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();
}
我认为您的问题在于没有将新创建的项添加到适配器的集合中。调用
expAdapter.notifyDataSetChanged()
似乎不起作用的原因是数据集实际上没有更改。您已将新项添加到数据库中,但未更新适配器的列表。因此,就适配器而言,项目列表没有更新。当您希望刷新数据时,应使用expAdapter.notifyDataSetChanged();非常感谢。正如您在我的代码的最后一行中所看到的,我尝试了这种方法,但没有成功。看起来问题在于我的两个数据库事务。不知何故,当我执行“get”查询时,我并没有得到新数据。可能“set”查询在“get”查询运行之前没有完成。我只需手动将对象从列表中删除并添加到列表中即可解决此问题。然后,正如Yume117所提到的,对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();
}