Search code examples
javaandroidarrayssqliteodata

How to convert from a List to Array ? Android


okay guys, here is the thing, I have one application consuming ODATA service, in SMP server, I'm getting this Data like this:

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

Okay I got this solution from web and could implement as list, and I need to store this entity which one is a List of customers and get the two attributes from this entity and save in my database so:

Entity Customer:Custormer_ID, Customer_Name

Here is my code to call my 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();
    }

And my 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);
    }

And off course my other methods, that is not important, so whats happening when I run my "for" in the sql call method, I get the size() of the list and the rows of the list and store the entire row in the one column of the database each time, so I got two different tables with the same values,

how can I change solve this problem instead of consume in list I need to consume in array ? or I need to create a method that get the list values and after a ,(coma) , create two differents objects to store these data ??

I took a long look in the internet and didn't find nothing, probably it's because i don't know yet, how so, I don't know for what I'm looking for it, I'm using the odata4j API and here is the link of the documentation, http://odata4j.org/v/0.7/javadoc/

I'm new on programming, so I'm really in trouble with this, any suggestions any helps will be truly, appreciate,

Thanks a lot and have a nice day !!!


Solution

  • You can add each entity to the `ArrayList' array by doing the following:

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

    This will allow you to access the data contained in the entity via getProperty() when inserted into the database.

    The following statement is also not needed, as the for each loop runs through every element in the list, thus for (OEntity entity : listEntities) will not execute if the list is empty.

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

    If you have multiple ODataConsumers, you have two choices, depending on your requirements (if I understand you question correctly):

    1. You can sequentially get each ODataConsumer, get the listEntities, and add it to the zmob_kunnr list, and after the list items are added to the database, clear the zmob_kunnr list, and call doInBackground with a new URL. This is what your current solution allows.

    It appears to need to know which property is associated with a URL when reading the values into the DB. You can use a POJO as a holder for the entity and its list of properties. You can now add and remove properties. Note that properties will be removed in the same order they where inserted.

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

    This will require a change to the list holding the entities:

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

    If you would like to add all the entities from the different URLs at the same time, you will need to have access to all the URLs when doInBackground is called. Something like this:

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

    You now have ALL of the entities associated with the list of URLs in zmob_kunnr. Before you can and can insert then into the DB like so:

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

    If each entity has a associated name, you can store the names in a map, where the key is the URL and the value the name.

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

    You can then, when running through the list of entities, do a look-up in the map to find the correct name:

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

    I hope this helps, if I misunderstood you just correct me in the comments.

    EDIT: Added list of URLs, holder and DB insert.