1

私はこのコードを持っています:

DatabaseHandler db = new DatabaseHandler(this);
System.out.println("Start - " + System.currentTimeMillis());
for (int i = 1; i < db.getChampsCount() + 1; i++) {
    String name = db.getChampInfo(i, "name");
    String title = db.getChampInfo(i, "title");
    String thumb = db.getChampInfo(i, "thumb");
    System.out.println("End - " + System.currentTimeMillis());
    [...]
}

この

String getChampInfo(int id, String col) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery("SELECT " + col + " FROM champions WHERE id = " + id, new String[] {});
    if (cursor != null)
        cursor.moveToFirst();

    db.close();
    return cursor.getString(0);
}

DatabaseHelper クラスの一部です

問題なく動作しますが、問題は実行に時間がかかりすぎることです (私の Android フォンでは 2089ms)。これらの文字列は UI の一部なので、別のスレッドに入れることはできないと思います。このコードをより速く実行するにはどうすればよいですか?

編集:正確に110行あります

4

2 に答える 2

4

個々のステートメントの代わりに、Single sql statement を使用しないでください。

Activity クラスに必要なすべての値を格納する ArrayList を 1 つ作成するだけです。

例えば:ArrayList<String> myData;

データベース ヘルパー クラスで、以下のような関数を 1 つ作成します。

 // TO get All Data of datanase which you want
public ArrayList<String> getAllData() {          
    ArrayList<String> subTitleList = null;         
    Cursor cursor = null;          
    try {              
    String queryString = "SELECT * FROM champions";              
        cursor =  db.rawQuery(queryString, null);              
        if (cursor != null && cursor.moveToFirst()) {                 
            subTitleList = new ArrayList<String>();                 
            do {                     
                String nextUser = new String(cursor.getString(cursor.getColumnIndex("name")));                     
                String nextUser = new String(cursor.getString(cursor.getColumnIndex("title")));                     
                String nextUser = new String(cursor.getString(cursor.getColumnIndex("thumb")));                     

                subTitleList.add(nextUser);                 
            } 
            while (cursor.moveToNext());   
            System.out.println("it comes in SubTitleList");
        }         
    } 
    catch (Exception e) {             
        e.printStackTrace();             
        subTitleList = null;         
    } 
    finally {             
        if (cursor != null && !cursor.isClosed()) {                 
            cursor.deactivate();                 
            cursor.close();                 
            cursor = null;             
        }             
        if(db != null){                 
            db.close();             
        }         
    }
    //System.out.println("SubTitleList is: "+subTitleList);
    return subTitleList;   
}

これで、アクティビティ クラスでこの関数を呼び出して、必要なすべてのデータを myData ArrayList から取得できます。

myData = db.getAllData(); // i think there is no need of any ID if you are fetching all the data.

あなたが私の主張を理解してくれることを願っています。

于 2013-01-24T04:13:41.753 に答える
2

あなたは間違いなくそれらをで実行することができますAsyncTask. あなたがしなければならないのは、データをタスクのパラメーターに渡すことです。それがわからない場合は、パラメーターを受け取るコンストラクターをタスクに持たせて、次のように呼び出します。

MyAsync ma = new MyAsync(stuff, stuff, stuff);
ma.execute();

タスクonPostExecute()では、バックグラウンドで実行されたクエリからデータを取得して、UI を更新できます。

また、他の人はちょっと正しいです。クエリを組み合わせることができれば最高ですが、そのようなテーブルではパフォーマンスが大幅に向上するわけではありません。少なくとも私はそうは思いません。

于 2013-01-24T04:03:57.390 に答える