2014-12-04 79 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()这样的字段;我不明白...

非常感谢!