[mysql]获取由下达订单 android SQlite Android

发布时间: 2016/10/20 0:24:33
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有本书标记在我的应用程序的选项,在这里使用进行排序在 SQLITE"按顺序"选择集的列的值 (ID) 喜欢︰ 按顺序 (ID = 1,5,4,3) 会让记录 1,5,4,3,整理出来,

但它显示 id = 1,2,3,4,5 在 listview 中的请任何一个帮助我。

databasehelper activity

public Cursor getBookMarks() {
    try{
        SQLiteDatabase db =myDataBase;// getReadableDatabase();
        SQLiteQueryBuilder qb1 = new SQLiteQueryBuilder();
        String [] sqlSelect1 = {"0 _id", KEY_NAME,KEY_PH_NO,KEY_ID}; 

        String sqlTables1 = TABLE_BOOKMARK;
        String strQuery1=KEY_PH_NO+"='1'";
        strOrder1=KEY_ID + "  DESC";
        qb1.setTables(sqlTables1);

        String strvalues="";

        Cursor curs = qb1.query(db, sqlSelect1, strQuery1, null,null, null, strOrder1);

        if(curs!=null)
        {
            curs.moveToFirst();
            if(curs.getCount()>=1)
            {
                do 
                {
                    strvalues+=""+curs.getString(curs.getColumnIndex(KEY_NAME))+",";
                }
                while (curs.moveToNext());

                if(strvalues.length()>=1)
                {
                    strvalues+="-1";
                    String [] sqlSelect = {"0 _id", "TB","Version","Book","Chapter","NKJ"};
                    String sqlTables = "hindibible";

                    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
                    qb.setTables(sqlTables);
                    String strOrderbook="Book" + " desc";

                    StringBuilder strQuery = new StringBuilder();
                    strQuery.append("ID IN (" +strvalues + ")"); 
                    StringBuilder strOrderBy = new StringBuilder();
                    strOrderBy.append(" CASE  ID ");
                    String [] arrIDs = strvalues.split(",");
                    for(int i=0;i<arrIDs.length;i++)
                    {
                        strOrderBy.append("  WHEN " + arrIDs[i] + "  THEN " + arrIDs[i]);
                    }

                    strOrderBy.append(" END");
                    Cursor bible = qb.query(db, sqlSelect, strQuery.toString(), null,null, null, strOrderBy.toString());

                    bible.moveToFirst();
                    return bible;
                }
            }
            else
            {
            }
        }
        else
        {
        }
    }catch (Exception e) {
        Log.d("setBookMark",e.toString());
    }
    return null;
}

解决方法 1:

我想这

if(curs!=null)
{
    List<Cursor> listCursor = new ArrayList<Cursor>();      
    curs.moveToFirst();

    if(curs.getCount()>=1)
    {
        do 
        {
            String strID = curs.getString(curs.getColumnIndex(KEY_NAME));
            String [] sqlSelect = {"0 _id", "TB", "Version", "Book", "Chapter", "NKJ"}; 
            String sqlTables = "hindibible";

            SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
            qb.setTables(sqlTables);

            StringBuilder strQuery = new StringBuilder();
            strQuery.append("ID = "+strID); 

            Cursor bible = qb.query(db, sqlSelect, strQuery.toString(), null,null, null,null);
            listCursor.add(bible);
        }while (curs.moveToNext());

        return listCursor;
    }
赞助商