简体   繁体   中英

Android - Fragment containg ListView, CursorAdapter using SQLite

I have a Fragment that contains a listView and button. When the button is clicked a DialogFragment will open where the user can input data into EditText fields. Once the user presses setPositiveButton it calls method in DBHelper to insert data into SQLite then dismisses dialogFragment

My problem, once it dismisses the ListView should update with the new input listing all future inputs. However I cant seem to get it to work. I have made a CursorAdapter. Does anyone know why it isnt working

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);
}

}

Query the database table notes, if sharedpref keyvalue username equals column_creator then return data, is my query right?

    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();
}

Finally attaching the ListView to my Fragment, I made a method I can call to popluate the 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();
}

When you dismiss the dialog say by pressing OK then you have to notify the cursor that data has been changed you have to update yourself..

Edit1:
Declare the cursor as data member of the class too :)
i want you to do something like the following Implementation:

public class Notes extends Fragment {

NotesControl control;
NotesAdapter adapter;
ListView listView;
Button add;
Cursor cursor;
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);

    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);

    control = new NotesControl(getActivity());
    control.open();
    populateList();
}

public void populateList(){
      cursor = control.listNotes();
      listView = (ListView) getView().findViewById(android.R.id.list);
      adapter = new NotesAdapter(getActivity(), cursor);

      listView.setAdapter(adapter);

    control.close();
}
@Override
public void onResume(){
    if(isDataUpdated)
   {
     cursor.requery();
     adapter.notifyDataSetChanged();
     Notes.isDataUpdated = false;
    }

}

Important
in your DialogFragment Ok button click event add the following snippet

Notes.isDataUpdated = true;

PS:
the method requery() is depricated. after getting the hang of this next logical step would be to user CursorLoaders

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM