2017-02-22 42 views
0

這將是一個關於性能的問題。首先對我的英語感到抱歉,但我們走吧,我有一個應用程序,它是一家餐廳的菜單。性能android在數據庫上的多重連接

它運作良好,但我想改善,我希望它足夠快,不會太慢。

例如:

在我第一次得到「!跳過XX框架應用程序可以做太多的工作在其主線程。」,所以我讀的最好的辦法就是不要把這麼多的大圖片並盡到數據庫中的一個線索或運行連接

這是我在片段代碼:

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 

    controller = new MDAlacarte(getActivity()); 
    int idCategoria = Integer.parseInt(getArguments().getString("idCategoria")); 
    int idSubcategoria = Integer.parseInt(getArguments().getString("idSubcategorias")); 

    buscarPratos(idCategoria,idSubcategoria); 

} 

private void buscarPratos(final int idCategoria, final int idSubcategoria) { 

    getActivity().runOnUiThread(new Runnable(){ 
     public void run() { 
      //If there are stories, add them to the table 

      pratosLista = controller.getPratosPorCategoriaAlacarte(idCategoria,idSubcategoria); 
      int tamanho = pratosLista.size(); 

      for (int i = 0; i < tamanho ; i++) { 


       pratoObj = new Prato(pratosLista.get(i).get("nome"), 
         pratosLista.get(i).get("descricao"), 
         pratosLista.get(i).get("codigo"), 
         pratosLista.get(i).get("categoria"), 
         pratosLista.get(i).get("preco"), 
         pratosLista.get(i).get("tipo"), 
         pratosLista.get(i).get("subcategoria")); 


       pratos.add(pratoObj); 
       nomePratos.add(pratosLista.get(i).get("nome")); 

      } 

     } 
    }); 
} 

但問題是,很慢,有時當我在categoria添加這麼多的標籤,如「飲料「即是巴西的」貝比達斯「,速度如此之慢要執行該操作,它就像8個選項卡,每個示例每個片段都爲每個選項卡執行此代碼。

這是最佳做法?這是一個列表視圖,我使ViewHolder模式具有良好的性能。

我錯過了什麼?

+0

更換getActivity().runOnUiThread(new Runnable(){...};你不需要'runOnUiThread'那裏。 .. –

+0

哼,爲什麼cricket_007? – Wavrik

+0

因爲那段代碼已經在'onViewCreated'之後的UI線程上了 –

回答

0

下車UI線程的工作,你可能想使用AsyncTaskhttps://developer.android.com/reference/android/os/AsyncTask.html

您可以簡單地通過這個

private void buscarPratos(final int idCategoria, final int idSubcategoria) { 
    new AsyncTask<Void, Void, Void>() { 
     @ Override 
     public void doInBackground() { 
      // If there are stories, add them to the table 

      pratosLista = controller.getPratosPorCategoriaAlacarte(idCategoria,idSubcategoria); 
      int tamanho = pratosLista.size(); 

      for (int i = 0; i < tamanho; i++) { 
       nome = pratosLista.get(i).get("nome") 
       pratoObj = new Prato(nome, 
         pratosLista.get(i).get("descricao"), 
         pratosLista.get(i).get("codigo"), 
         pratosLista.get(i).get("categoria"), 
         pratosLista.get(i).get("preco"), 
         pratosLista.get(i).get("tipo"), 
         pratosLista.get(i).get("subcategoria")); 

       pratos.add(pratoObj); 
       nomePratos.add(nome); 
      } 
     } 
    }).execute(); 
} 
+0

我使用Asynctask與Internet進行連接,但不能用於SQLITE操作。 – Wavrik

+0

謝謝,但現在我面臨另一個問題,我的Asynctask函數在我的適配器執行之前,從我的片段創建視圖執行: 我該如何執行此操作? – Wavrik

+0

好吧,多一點的代碼會很有用。你能否重申你的問題? – Androbin