我使用自定义SimpleCursorAdapter填充ListView。
我有一个删除功能,可以从支持ListView的数据库中删除某个人。
我无法确定放置notifyDataSetChanged()
的位置,以便我的列表刷新以显示此人已被移除。
我正在使用SwipeRefreshLayout,如果我滑动,则会显示此人已被删除,但我希望列表在删除此人时自动更新。
我的代码在哪里使用notifyDataSetChanged()
?
Home.java
public class Home extends Fragment implements TabChanger{
private View rootView;
private ViewPager myViewPager;
private SwipeRefreshLayout personSwipeRefresh;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.home, container, false);
myViewPager = (ViewPager) getActivity().findViewById(R.id.pager);
personSwipeRefresh = (SwipeRefreshLayout) rootView.findViewById(R.id.person_swipe_refresh);
return rootView;
}
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
drawThePersonView();
personSwipeRefresh.setColorSchemeColors(Color.parseColor(Constants.RED), Color.parseColor(Constants.ORANGE), Color.parseColor(Constants.YELLOW), Color.parseColor(Constants.GREEN), Color.parseColor(Constants.BLUE), Color.parseColor(Constants.INDIGO), Color.parseColor(Constants.VIOLET));
personSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
personSwipeRefresh.setRefreshing(false);
drawThePersonView();
}
});
}
private void drawThePersonView(){
DatabaseHelper myDBHelper = new DatabaseHelper(getActivity());
Cursor personCursor = myDBHelper.getPersonsCursor();
String[] fromColumns = {"_id","studentID","firstname","lastname","object_id"};
int[] toViews = {R.id.person_number_textview, R.id.person_id_textview};
FragmentManager fragMan = this.getFragmentManager();
CustomSimpleCursorAdapter mySimpleCursorAdapter = new CustomSimpleCursorAdapter(this, fragMan, getActivity(), R.layout.person_layout, personCursor, fromColumns, toViews, 0);
// Replace the _id column with a person count
mySimpleCursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
String counter = Integer.toString((cursor.getPosition()+1));
TextView modifiedTextView = (TextView) view;
if(columnIndex == 0){
modifiedTextView.setText(counter);
return true;
}
return false;
}
});
ListView myListView = (ListView) rootView.findViewById(R.id.person_row);
// Draw the list
myListView.setAdapter(mySimpleCursorAdapter);
}
// Pass me a tab index (see Constants.java) and I'll return a refrence to that tab.
private String getFragmentTag(int tagID){
return "android:switcher:" + R.id.pager + ":" + tagID;
}
@Override
public void changeTab(int tab) {
myViewPager.setCurrentItem(tab);
}
}
CustomSimpleCursorAdapter.java
public class CustomSimpleCursorAdapter extends SimpleCursorAdapter {
private Context context;
private Cursor cursor;
public CustomSimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags) {
super(context, layout, cursor, from, to, flags);
this.context = context;
this.cursor = cursor;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
// Set alternating rows to different colors.
View row = super.getView(position, convertView, parent);
if(position % 2 == 0){
row.setBackgroundColor(Color.parseColor(Constants.WHITE));
} else {
row.setBackgroundColor(Color.parseColor(Constants.LIGHTGREY));
}
// When the delete icon is clicked, delete from the database.
// Then, refresh the view.
ImageView deleteButton = (ImageView) row.findViewById(R.id.person_delete_button);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cursor.moveToPosition(position);
final String personID = cursor.getString(1);
Toast statusToast = Toast.makeText(context, "Deleting person " + personID + " ...", Toast.LENGTH_SHORT);
statusToast.show();
DatabaseHelper myDBHelper = new DatabaseHelper(context);
myDBHelper.deletePerson(personID);
}
});
return row;
}
}