Android-SQLite rawQuery列表视图

Android-SQLite rawQuery列表视图,android,android-fragments,android-listview,android-sqlite,Android,Android Fragments,Android Listview,Android Sqlite,我正在尝试查询唯一属于他的所有用户数据。通过notes表中用户的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}, n

我正在尝试查询唯一属于他的所有用户数据。通过notes表中用户的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()将在CreateView中调用

    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);
}
这是一个空指针。我传递的数据是错误的吗
Cursor=control.listNotes()

您的
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;
    }
试试这个:

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;
}
public List getAllTask(int userID){
String query=“从tablename中选择*,其中ID=“+userID;
cursor=db.query();//在此处执行代码
intid=cursor.getInt(cursor.getColumnIndex(tablename.id));
列表任务=新建ArrayList();
String query=“从tableTask中选择*,其中ID=“+ID”;
cursor=db.query()//
如果(光标!=null){
cursor.movetofirst();
//请在这里输入您的代码
做{
字符串任务=游标。获取。。。。。。
task.add(任务);
}while(cursor.movetonext);
}
返回任务;
}

它无法识别getActivity()。getContentResolver()ListNotes与我的所有查询都在一个类中,以获取不在片段中的用户数据OK,因此您需要一个上下文来调用getContentResolver(),就像您的上下文一样。getContentResolver()…嗯,那是什么上下文?我所拥有的只是来自DBHelper和Instance的SQLItedatabase的引用。我如何将它传递给我的listviewOk,这样你就可以使用:游标c=db.query(你的ToToToTableName,新字符串[]{help.COLUMN_TITLE,help.COLUMN_BODY,help.COLUMN_DATE},help.COLUMN_USER_ID+“=?”,新字符串[]{String.valueOf(userId)},null,null,null);那么我是否需要更改populateList如何获得此信息?我是否需要光标?您可以创建自己的用户类,并在其中添加一个字段
private ArrayList mTask;
。这样在ListView中显示数据会容易得多…我创建的用户没有getter,我只是使用了put()要将每个用户添加为新行,您已经有了2个很好的答案。您仍在解决此问题吗?如果是,您现在有哪些与发布的问题相关的具体问题?@theOriginalAndroid我将更新我的代码,Cursor Cursor=control.listNotes();是一个空点如果您在答案中发布了示例代码,那么如果您尝试一些东西并发布您编写的所有相关代码(无论是否有bug),这将有所帮助,代码应该与问题相关。这样,您将在这个博客社区中获得更好的声誉/形象。@TheOriginalAndroid我已经更新了我的代码,并显示了我的所有代码。如果可能的话,我想尝试使用ListFragment。我不熟悉DatabaseHelper,因为您非常依赖它。请确保临时数据库和表是可用的在使用listNotes()查询表之前创建。使用SQLite调查DatabaseHelper。下面是对它的讨论,希望能让您有所了解。