Android - SQLite rawQuery Listview

时间:2015-03-06 09:15:51

标签: android android-fragments android-listview android-sqlite

我正在尝试查询属于他的所有用户数据。通过注释表中的用户的FK_ID。

// Listing all notes

public Cursor listNotes() {
    SQLiteDatabase db = help.getReadableDatabase();
    Cursor c = db.query(help.NOTE_TABLE, new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE}, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }

   return c;

 }



// Count how many Notes user has
public int NoteCount() {

    String countQuery = "SELECT  * FROM " + help.NOTE_TABLE;
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}
// Updating single contact
public void editNote(long id, String title, String body) {

    ContentValues edit = new ContentValues();
    edit.put(help.COLUMN_TITLE, title);
    edit.put(help.COLUMN_BODY, body);

    open();
    db.update(help.NOTE_TABLE, edit, help.NOTES_ID + "=" + id, null);
    close();
}

// Deleting single note
public void deleteNote(long id) {
    open();
    db.delete(help.NOTE_TABLE, help.NOTES_ID + "=" + id, null);
    close();
}

}

我有一个标签,然后将所有用户数据唯一地返回给他。此选项卡是一个片段。方法populateList()将被调用onCreateView

    public void populateList(){

    Cursor cursor = control.listNotes();
    getActivity().startManagingCursor(cursor);

    //Mapping the fields cursor to text views
    String[] fields = new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE};
    int [] text = new int[] {R.id.item_title,R.id.item_body, R.id.item_date};
    adapter = new SimpleCursorAdapter(getActivity(),R.layout.list_layout,cursor, fields, text,0);

    //Calling list object instance
    listView = (ListView) getView().findViewById(android.R.id.list);
    adapter.notifyDataSetChanged();
    listView.setAdapter(adapter);
}

这是一个空指针。我传递数据错了     游标光标= control.listNotes();

2 个答案:

答案 0 :(得分:0)

您的listNotes方法应如下所示:

public Cursor listNotes(long userId) {
        Cursor c = getActivity().getContentResolver().query(yourTodoTableURI, new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE}, help.COLUMN_USER_ID + " = ?", new String[]{String.valueOf(userId)} , null);
        return c;
    }

答案 1 :(得分:0)

试试这个:

public List<String> getAllTask(int userID){  

      String query = "Select * from tablename where ID =" + userID; 
          cursor = db.query(); //do your code here
      int id = cursor.getInt(cursor.getColumnIndex(tablename.ID));

      List<String> task = new ArrayList<String>();

      String query = "Select * from tableTask where ID =" + id";
      cursor = db.query(); //
      if (cursor != null) {
          cursor.movetofirst();
          // do your code here

          do{
          String task = cursor.get......

          task.add(task);
          }while(cursor.movetonext);
      }

return task;
}