通过给定顺序获得Android SQlite Android的订单

时间:2015-06-12 07:37:30

标签: java android mysql database sqlite

我的应用程序中有一个书签选项,此处用#34排序SQLITE;按顺序排序"使用选定的一组列值(ID),如:order by(ID = 1,5,4,3),所以我会按顺序获得记录1,5,4,3,

但是在listview中显示id = 1,2,3,4,5请任何人帮助我。

databasehelper活动

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 个答案:

答案 0 :(得分:0)

我正在尝试这个

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