【发布时间】:2015-05-15 01:00:03
【问题描述】:
我有一个包含 listView 和按钮的片段。单击按钮时,将打开一个 DialogFragment,用户可以在其中将数据输入到 EditText 字段中。一旦用户按下setPositiveButton,它就会调用 DBHelper 中的方法将数据插入 SQLite,然后关闭 dialogFragment
我的问题,一旦它关闭 ListView 应该使用列出所有未来输入的新输入进行更新。但是我似乎无法让它工作。我制作了一个 CursorAdapter。有谁知道它为什么不起作用
public class NotesAdapter extends CursorAdapter {
DatabaseHelper help;
private Cursor c;
public NotesAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
c = cursor;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_layout, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
//View
TextView title = (TextView) view.findViewById(R.id.item_title);
TextView body = (TextView) view.findViewById(R.id.item_body);
TextView date = (TextView) view.findViewById(R.id.item_date);
//Columns to bound into view
String getTitle = cursor.getString(cursor.getColumnIndexOrThrow("title"));
String getBody = cursor.getString(cursor.getColumnIndexOrThrow("body"));
String getDate = cursor.getString(cursor.getColumnIndexOrThrow("date"));
title.setText(getTitle);
body.setText(getBody);
date.setText(getDate);
}
}
查询数据库表注释,如果sharedpref keyvalue username等于column_creator则返回数据,我的查询对吗?
public Cursor listNotes() {
SQLiteDatabase db = help.getReadableDatabase();
String username = session.getUser();
// Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(listNotes))
return db.rawQuery("SELECT * FROM " +help.NOTE_TABLE+ "WHERE " +help.CREATOR+ "= '" + username + "' COLLATE NOCASE", null);
}
public class Notes extends Fragment {
NotesControl control;
NotesAdapter adapter;
ListView listView;
Button add;
public static boolean isDataUpdated=false;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_notes, container, false);
add = (Button) view.findViewById(R.id.addbtn);
control = new NotesControl(getActivity());
control.open();
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
NotesDialog dialog = new NotesDialog();
dialog.show(getChildFragmentManager(), "Dialog Fragment");
}
});
return view;
}
public void onActivityCreate(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
populateList();
}
最后将 ListView 附加到我的 Fragment,我创建了一个可以调用来填充 List 的方法
public void populateList(){
Cursor cursor = control.listNotes();
listView = (ListView) getView().findViewById(android.R.id.list);
NotesAdapter adapter = new NotesAdapter(getActivity(), cursor);
listView.setAdapter(adapter);
control.close();
}
【问题讨论】:
标签: java android android-fragments android-listview android-sqlite