我將數據從選項卡片段插入到sqlite數據庫中。我的問題是一旦數據被插入或從列表中刪除,更新後的列表僅在應用程序重新啓動後纔會顯示。新數據添加到列表後,如何使片段刷新?如何在添加數據後刷新片段中的列表視圖?
任何幫助,這將不勝感激。
public Tab1() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static Tab1 newInstance(String param1, String param2) {
Tab1 fragment = new Tab1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
//load tasks into the list
private void loadTaskList() {
ArrayList<String> taskList = dbHelper.getTaskList();
if (mAdapter == null) {
mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.row, R.id.task_title, taskList);
lstTask.setAdapter(mAdapter);
} else {
mAdapter.clear();
mAdapter.addAll(taskList);
mAdapter.notifyDataSetChanged();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View frag1 = inflater.inflate(R.layout.fragment_tab1, container, false);
dbHelper = new DbHelper(getActivity());
lstTask = (ListView)frag1.findViewById(R.id.lstTask);
loadTaskList();
return frag1;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
}