android开发分享从Sqlite查询遍历行

我有一个表格布局,我想填充从数据库查询的结果。 我使用全选和查询返回四行数据。

我使用这段代码填充表格行内的文字。

Cursor c = null; c = dh.getAlternative2(); startManagingCursor(c); // the desired columns to be bound String[] columns = new String[] {DataHelper.KEY_ALT}; // the XML defined views which the data will be bound to int[] to = new int[] { R.id.name_entry}; SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, c, columns, to); this.setListAdapter(mAdapter); 

我希望能够分开KEY_ALT的四个不同的值,并select他们去的地方。 我想让他们填充四个不同的textviews,而不是我上面的例子中的一个。 我怎样才能遍历结果光标?

问候,AK

    您可以使用下面的代码来通过游标并将它们存储在string数组中,然后可以在四个文本视图中设置它们

     String array[] = new String[cursor.getCount()]; i = 0; cursor.moveToFirst(); while (!cursor.isAfterLast()) { array[i] = cursor.getString(0); i++; cursor.moveToNext(); } 

    数据库查询返回的Cursor对象位于第一个条目之前 ,因此迭代可以简化为:

     while (cursor.moveToNext()) { // Extract data. } 

    SQLiteDatabase引用。

     for (boolean hasItem = cursor.moveToFirst(); hasItem; hasItem = cursor.moveToNext()) { // use cursor to work with current item } 

    迭代可以通过以下方式完成:

     Cursor cur = sampleDB.rawQuery("SELECT * FROM " + Constants.TABLE_NAME, null); ArrayList temp = new ArrayList(); if (cur != null) { if (cur.moveToFirst()) { do { temp.add(cur.getString(cur.getColumnIndex("Title"))); // "Title" is the field name(column) of the Table } while (cur.moveToNext()); } } 

    我同意chiranjib,我的代码如下:

     if(cursor != null && cursor.getCount() > 0){ cursor.moveToFirst(); do{ //do logic with cursor. }while(cursor.moveToNext()); } 

    find一个非常简单的方法来遍历游标

    以上就是android开发分享从Sqlite查询遍历行相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

     for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){ // access the curosr DatabaseUtils.dumpCurrentRowToString(cursor); final long id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)); } 

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/514311.html

      (0)
      上一篇 2020年11月30日
      下一篇 2020年11月30日

      精彩推荐