2014-12-04 78 views
1

好,大家好,這裏是事,我已經一個應用程序消耗ODATA service,在SMP server,我得到這個數據是這樣的:如何將列表轉換爲數組? Android的

public class callService extends AsyncTask<Void, Void, ArrayList<String>> 
    { 
     public ArrayList<String> doInBackground(Void... params) 
     { 
      ODataConsumer c = ODataJerseyConsumer.create("http://MyUrlService:8080"); 
      List<OEntity> listEntities = c.getEntities("MYENTITYTOCONSUME").execute().toList(); 
      System.out.println("Size" + listEntities.size()); 
      if (listEntities.size() > 0) 
      {    
       for (OEntity entity : listEntities) 
       { 
       zmob_kunnr.add((String) entity.getProperty("Name1").getValue() 
        + " - " 
       + entity.getProperty("Kunnr").getValue().toString()); 
       } 
      } 
      return zmob_kunnr; 
     } 
     protected void onPostExecute(ArrayList<String> result) 
     { 
     super.onPostExecute(result); 
     adapter = new ArrayAdapter<String>(ConsumoKnuur.this, android.R.layout.simple_list_item_1, result); 
     list.setAdapter(adapter); 
     } 
    } 

好吧,我從網絡這個解決方案,並可以實現爲列表,我需要存儲這個實體,一個是客戶的名單,並從該實體的兩個屬性,並保存在我的數據庫中,以便:

Entity Customer:Custormer_ID, Customer_Name 

這裏是我的代碼來調用我的SQLite:

public void sqlite() 
    { 
     sql_obj.open(); 
     sql_obj.deleteAll();  
      for(int i=0; i < zmob_kunnr.size(); i++) 
      { 
       sql_obj.insert(zmob_kunnr.get(i).toString(), zmob_kunnr.get(i).toString()); 
      } 
     sql_obj.close(); 
    } 

而且我的SQLite:

private static final String TABLE_CLIENTE = "CREATE TABLE " 
      + TB_CLIENTE 
      + "(ID_CLIENTE INTEGER PRIMARY KEY AUTOINCREMENT, " //Id for controller my logics 
      + " Kunnr TEXT , " //customer ID 
      + " Name1 TEXT);"; //customer_name 
public long insert(String name1, String Kunnr) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put("Name1", Name1); //Customer_Name 
     initialValues.put("Kunnr", Kunnr); //Customer_ID 
     return database.insert(TB_CLIENTE, null, initialValues); 
    } 

和關閉過程我其他的方法,那並不重要,那麼什麼時候我運行發生的我「爲」在SQL調用方法,我得到的名單size()和行的列表,並將整行存儲在數據庫的每一列中,所以我得到了兩個不同的表,具有相同的值,

我該如何改變解決這個問題,而不是消耗在列表中我需要在數組中消耗?或者我需要創建一個獲取列表值的方法,並在,(昏迷)後創建兩個不同的對象來存儲這些數據?

我把在互聯網上長時間看,並沒有發現什麼,大概是因爲我還不知道呢,怎麼會這樣,我不知道我在尋找什麼它做什麼,我使用在odata4j API,這裏是文件的我對編程新的鏈接,http://odata4j.org/v/0.7/javadoc/

,所以我真的在這個麻煩,有什麼建議的任何幫助將是真正,欣賞,

非常感謝祝你有愉快的一天 !!!

+0

如何定義'zmob_kunnr'?如果將zmob_kunnr的類型更改爲'ArrayList ',您可以在'insert'語句中使用'sql_obj.insert(zmob_kunnr.get(i).getProperty(「Name1」)。getValue(),zmob_kunnr.get ).getProperty(「Kunnr」)。getValue()。toString());' – Ian2thedv 2014-12-04 13:28:03

+0

你好Nayeem非常感謝你的有益答案,我理解你的邏輯,現在有道理,我改變了我的mob_kunnr到ArrayList ,我也改變了來自zmob_kunnr的所有其他調用,但是zmob_kunnr.add方法給了我「add」一個問題(類型ArrayList 中的方法add(OEntity)不適用於參數(String))更改爲addAll,如果我改變仍然有問題,並告訴我chande添加 – 2014-12-04 16:19:44

+0

哈哈以及我不是Nayeem,但你需要將'for'循環的主體改爲'zmob_kunnr.add(實體)' – Ian2thedv 2014-12-05 06:21:52

回答

1

您可以通過執行以下每個實體添加到'ArrayList的」點陣:

for (OEntity entity : listEntities) { 
    zmob_kunnr.add(entity); 
} 

這將讓你在插入數據庫通過getProperty()訪問實體中包含的數據。

以下語句也不需要,因爲for each循環遍歷列表中的每個元素,因此如果列表爲空,for (OEntity entity : listEntities)將不會執行。

if (listEntities.size() > 0) {    
    ... 
} 

如果您有多個ODataConsumers,你有兩個選擇,根據您的要求(如果我理解你的問題正確):

  1. 您可以依次得到各ODataConsumer,得到了listEntities,並添加它的zmob_kunnr名單,並經過列表項添加到數據庫中,清除zmob_kunnr清單和通話doInBackground一個新的URL。這是你當前的解決方案允許的。

在將值讀入數據庫時​​,似乎需要知道哪個屬性與URL關聯。您可以使用POJO作爲實體及其屬性列表的持有者。您現在可以添加和刪除屬性。請注意,屬性將按照插入的順序刪除。

public class OEntityHolder { 
    private final OEntity entity; 
    private Queue<String> properties; 

    public OEntityHolder(OEntity entity) { 
     this.entity = entity; 
     this.properties = new LinkedBlockingQueue<>(); 
    } 

    public OEntity getEntity() { 
     return this.entity; 
    } 

    public void addProperty(String property) { 
     this.properties.add(property); 
    } 

    public void removeProperty() { 
     this.properties.poll(); 
    } 
} 

這將需要改變列表拿着entities

ArrayList<OEntityHolder> zmob_entity_holders = new ArrayList<>(); 

如果你想添加的所有從在同一時間不同的網址entities,你將需要訪問當調用doInBackground時,所有的URL。事情是這樣的:

public ArrayList<OEntityHolder> doInBackground(Void... params) { 
    String [][] urls = {{"http:MyUrl/ZMOB_FECODSet", "Name1", "Fecod"}, 
         {"http:MyUrl/ZMOB_OTEILSet", "Name2", "Oteil"}, 
         {"http:MyUrl/ZMOB_KUNNRSet", "Name3", "Kunnr"}, 
         {"http:MyUrl/ZMOB_BAULTSet", "Name4", "Bault"}}; 
    for (String [] urlProp:urls) { 
     //Here you get the list of entities from the url 
     List<OEntity> listEntities = ODataJerseyConsumer.create(urlProp[0]).getEntities("MYENTITYTOCONSUME").execute().toList(); 
     for (OEntity entity:listEntities) { 
      OEntityHolder holder = new OEntityHolder(entity); 
      for (int i = 1; i < urlProp.length; i++) 
       holder.addProperty(urlProp[i]); 
      zmob_entity_holders.add(holder); 
     } 
    } 
    //At this point, all of the entities associated with the list of URLS will be added to the list 
    return zmob_entity_holders; 
} 

現在都與網址在zmob_kunnr列表相關的實體。之前,你可以和插入然後進入DB像這樣:

for (OEntityHolder holder : zmob_entity_holders) { 
    sql_obj.insert(holder.getEntity().getProperty(holder.removeProperty()).toString(), holder.getEntity().getProperty(holder.removeProperty()).toString()); 
} 

如果每個實體有關聯的名稱,可以在名稱中存儲的地圖,其中key是URL和value名稱。

HashMap<String, String> urlEntityNames = new HashMap<>(); 
urlEntityNames.put("http://MyUrlService:8080", "MYENTITYTOCONSUME"); 
...//Add more URLs and entity names 

然後,您可以通過實體的名單運行時,請在地圖上查找,找到正確的名稱:

List<OEntity> listEntities = ODataJerseyConsumer.create(url).getEntities(urlEntityNames.get(url)).execute().toList(); 

我希望這可以幫助,如果我誤解你只是糾正我的評論。

編輯:添加URL,持有人和數據庫插入的列表。

+0

哦,非常感謝也@ lan2thedv,這是exaclty也是我被nedding感謝你們這兩個... !!! – 2014-12-06 03:12:57

+0

是的,答案將所有實體添加到'zmob_kunnr'。我現在看到'ZMOB_KUNNRSet'和'zmob_kunnr'列表之間的關聯,但我不確定我是否理解正確。你想從http實體添加:MyUrl/ZMOB_KUNNRSet爲'zmob_kunnr',HTTP:MyUrl/ZMOB_OTEILSet爲'zmob_oteil'等,並就其性質發生變化?例如:http:MyUrl/ZMOB_OTEILSet - 'entity.getProperty(「Oteil」)'? – Ian2thedv 2014-12-08 06:26:45

+0

好的,我明白了,我更新了答案。不幸的是,由於缺乏關於您完整問題或解釋的知識,我無法進一步幫助您。如果還有其他的事情你正在努力,我不能幫你,發佈一個新的問題,它會得到更多的關注。祝你好運! – Ian2thedv 2014-12-08 07:03:41

0

我想我找到了解決辦法,但我的日誌貓,是給一個例外,我對我的第二個doInBackgroundBault(材料)的任何updtades,

public class callServiceCliente extends AsyncTask<Void, Void, ArrayList<OEntity>> { 
    protected void onPreExecute() { 
     progressC = ProgressDialog.show(Atualizar_Dados.this, "Aguarde...", "Atualizando Clientes", true, true); 
    } 

    public ArrayList<OEntity> doInBackground(Void... params) { 
     ODataConsumer ccli = ODataJerseyConsumer.create(URL); 
     List<OEntity> listEntitiesKunnr = ccli.getEntities("ZMOB_KUNNRSet").execute().toList(); 

     System.out.println("Size" + listEntitiesKunnr.size()); 

     for (OEntity entityKunnr : listEntitiesKunnr) { 
      zmob_kunnr.add(entityKunnr); 
     } 
     return zmob_kunnr; 
    } 

    protected void onPostExecute(ArrayList<OEntity> kunnr) { 
     super.onPostExecute(kunnr); 
     try { 
      sql_obj.open(); 
      sql_obj.deleteAll(); 

      for (int k = 0; k < zmob_kunnr.size(); k++) { 
       sql_obj.insertCliente(zmob_kunnr.get(k).getProperty("Kunnr").getValue().toString().toUpperCase(), zmob_kunnr.get(k).getProperty("Name1").getValue().toString().toUpperCase()); 
      } 
      sql_obj.close(); 
     } catch (Exception e) { 
     } 
     try { 
      clienteAdapter = new ArrayAdapter<OEntity>(Atualizar_Dados.this, android.R.layout.simple_list_item_1, kunnr); 
      listCliente.setAdapter(clienteAdapter); 

     } catch (Exception eq) { 
     } 
     progressC.dismiss(); 
     new callServiceMaterial().execute(); 
    } 
} 

public class callServiceMaterial extends AsyncTask<Void, Void, ArrayList<OEntity>> { 
    protected void onPreExecute() { 
     progressM = ProgressDialog.show(Atualizar_Dados.this, "Aguarde...", "Atualizando Materiais", true, true); 
    } 

    public ArrayList<OEntity> doInBackground(Void... params) { 
     ODataConsumer cmat = ODataJerseyConsumer.create(URL); 
     List<OEntity> listEntitiesBault = cmat.getEntities("ZMOB_BAULTSet").filter("IErsda eq '20141101'").execute().toList(); 
     System.out.println("Size" + listEntitiesBault.size()); 
     for (OEntity entityBault : listEntitiesBault) { 
      zmob_bault.add(entityBault); 
     } 
     return zmob_bault; 
    } 

    protected void onPostExecute(ArrayList<OEntity> bault) { 
     super.onPostExecute(bault); 
     try { 
      sql_obj.open(); 
      sql_obj.deleteAll(); 
      for (int b = 0; b < zmob_bault.size(); b++) { 
       sql_obj.insertMaterial(zmob_bault.get(b).getProperty("Matnr").getValue().toString().toUpperCase(), zmob_bault.get(b).getProperty("Maktxt").getValue().toString().toUpperCase()); 
      } 
      sql_obj.close(); 
     } catch (Exception e) { 
     } 
     try { 
      materialAdapter = new ArrayAdapter<OEntity>(Atualizar_Dados.this, android.R.layout.simple_list_item_1, bault); 
      listMaterial.setAdapter(clienteAdapter); 

     } catch (Exception eq) { 
     } 
     progressM.dismiss(); 
     new callServiceProblema().execute(); 
    } 
} 

public class callServiceProblema extends AsyncTask<Void, Void, ArrayList<OEntity>> { 
    protected void onPreExecute() { 
     progressProb = ProgressDialog.show(Atualizar_Dados.this, "Aguarde...", "Atualizando Problemas", true, true); 
    } 

    public ArrayList<OEntity> doInBackground(Void... params) { 
     ODataConsumer cprob = ODataJerseyConsumer.create(URL); 
     List<OEntity> listEntitiesFecod = cprob.getEntities("ZMOB_FECODSet").execute().toList(); 

     System.out.println("Size" + listEntitiesFecod.size()); 
     for (OEntity entityFecod : listEntitiesFecod) { 
      zmob_fecod.add(entityFecod); 
     } 
     return zmob_fecod; 
    } 

    protected void onPostExecute(ArrayList<OEntity> fecod) { 
     super.onPostExecute(fecod); 
     try { 
      sql_obj.open(); 
      sql_obj.deleteAll(); 
      for (int f = 0; f < zmob_fecod.size(); f++) { 
       sql_obj.insertProblema(zmob_fecod.get(f).getProperty("Fecod").getValue().toString().toUpperCase(), zmob_fecod.get(f).getProperty("Kurztext").getValue().toString().toUpperCase()); 
      } 
      sql_obj.close(); 
     } catch (Exception e) { 
     } 
     try { 
      problemaAdapter = new ArrayAdapter<OEntity>(Atualizar_Dados.this, android.R.layout.simple_list_item_1, fecod); 
      listProblema.setAdapter(problemaAdapter); 

     } catch (Exception eq) { 
     } 
     progressProb.dismiss(); 
     new callServiceProcedencia().execute(); 
    } 
} 

public class callServiceProcedencia extends AsyncTask<Void, Void, ArrayList<OEntity>> { 
    protected void onPreExecute() { 
     progressProc = ProgressDialog.show(Atualizar_Dados.this, "Aguarde...", "Atualizando base de dados", true, true); 
    } 

    public ArrayList<OEntity> doInBackground(Void... params) { 
     ODataConsumer c = ODataJerseyConsumer.create(URL); 
     List<OEntity> listEntitiesProcedencia = c.getEntities("ZMOB_OTEILSet").execute().toList(); 
     System.out.println("Size" + listEntitiesProcedencia.size()); 
     for (OEntity entityProcedencia : listEntitiesProcedencia) { 
      zmob_oteil.add(entityProcedencia); 
     } 
     return zmob_oteil; 
    } 

    protected void onPostExecute(ArrayList<OEntity> oteil) { 
     super.onPostExecute(oteil); 
     try { 
      sql_obj.open(); 
      sql_obj.deleteAll(); 

      for (int o = 0; o < zmob_oteil.size(); o++) { 
       sql_obj.insertCliente(zmob_oteil.get(o).getProperty("Fecod").getValue().toString().toUpperCase(), zmob_oteil.get(o).getProperty("Kurztext").getValue().toString().toUpperCase()); 
      } 
      sql_obj.close(); 
     } catch (Exception e) { 
     } 
     try { 
      procedenciaAdapter = new ArrayAdapter<OEntity>(Atualizar_Dados.this, android.R.layout.simple_list_item_1, oteil); 
      // listCliente.setAdapter(clienteAdapter); 

     } catch (Exception eq) { 
     } 
     progressProc.show(Atualizar_Dados.this, "Finalizado", "Base de dados atualizada", true, true).dismiss(); 
     Toast.makeText(Atualizar_Dados.this, "Base de dados atualizada com sucesso", Toast.LENGTH_LONG).show(); 
    } 
} 

好了,這裏是我找到的解決方案,我不能插入你的解決方案,因爲當我把inser.add(實體),他們沒有顯示我的屬性,但如果你有更好的方式來做我所做的,我將非常感激,

順便說一句,我需要通過filter()中的範圍日期查詢這些消耗。像我這裏做的那樣...

List listEntitiesBault = cmat.getEntities(「ZMOB_BAULTSet」)。filter(「IErsda eq'20141101'」)。execute()。toList();但沒有工作,所以我沒有任何想法,爲什麼,我在互聯網上看到了幾個關閉解決方案,並看到像.top(1)和.first()這樣的字段;我不明白...

非常感謝!