我想更新我的可扩展列表视图以查询我的数据库并在我从另一个片段添加用户后添加项目。基本上,我的用户有很多事件,我希望将该用户添加到可扩展列表视图中的每个组,具体取决于他们拥有的事件
例如:事件1,2,3在explist组1中,事件4,5,6在explist组2中,事件7,8,9在explist组3中。 如果用户有事件1,2,5,9,则应将用户添加到所有3个组中,但不在explistview的组1中重复。
我有一个数据库后端,它将获取所有用户以及获取最近添加的用户。如何在添加或编辑用户时将用户插入可扩展列表视图并基本上动态刷新我的explistview?我现在从我的添加用户片段向我的listview片段发送确认消息,但是当我通过活动发送确认时,我的程序崩溃。
代码:
添加用户片段:将用户添加到数据库,然后发送列表片段确认以更新explistview。
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnAddAthlete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Athlete athlete = new Athlete();
athlete.setFirstName(editFirstName.getText().toString());
athlete.setLastName(editLastName.getText().toString());
athlete.setAge(Integer.parseInt(editAge.getText().toString()));
athlete.setEvents(athleteEvents);
athlete.setGrade(editGrade.getText().toString());
db = new dbHelper(getActivity());
db.insertAthlete(athlete);
db.close();
Toast.makeText(getActivity(), athlete.getFirstName() + " " + athlete.getLastName() + " added!", Toast.LENGTH_SHORT).show();
AthletesList athletesList = new AthletesList();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frameLayout, athletesList, "athletesList").commit();
communicator.send("Athlete Added!");
}
});
}
主要活动:通知explistview片段与用户进行更新。 我不知道如何设置片段标签btw。
@Override
public void send(String confirmation) {
AthletesList athletesList = (AthletesList) getSupportFragmentManager().findFragmentByTag("athleteList");
athletesList.updateListView();
}
ExpListView用户片段:根据事件将用户列表包含到组中
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_athletes_list, container, false);
initializeVar();
setupToolbar();
listViewItems = addExpListViewItems();
adapterWhite = new ExpListViewAdapterWhite(getActivity(), listViewItems);
expListView.setAdapter(adapterWhite);
expListView.setOnGroupClickListener(this);
expListView.setOnChildClickListener(this);
return view;
}
// Adds items to explistview as of now, which is hardcoded
// Categories will be hardcoded but not children
// Prolly shouldn't even need this for children once I can just query the database
public ArrayList<listViewParent> addExpListViewItems(){
ArrayList<listViewParent> listViewItems = new ArrayList<listViewParent>();
for(int i = 0; i < 3; i++) {
// Parents that hold category and holds children which are subcategories
listViewParent category = new listViewParent();
if(i == 0) {
category.setTitle("4 x 400");
category.setImage(R.drawable.icon_relay);
category.setChildren(new ArrayList<listViewChild>());
listViewChild child = new listViewChild();
child.setTitle("Mike");
child.setImage(R.drawable.icon_blank);
category.getChildren().add(child);
}
listViewItems.add(i, category);
}
return listViewItems;
}
public void updateListView(){
// add child to explistview and update view so screen shows new user
}