Search code examples
androidarraysandroid-listviewksoap2

Display String arrayfrom Soap Webservice to listview Android


Im trying to display my array soap action result to listview. I managed to retrieve the items and store it as string, however, I cant seem to put all of the string into the listview. What am i doing wrongly?? When I Log.d, it displays everything in the correct order (Which is what I want). However when I add it to my SearchResults, it only display the last details.

 try
            {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapObject response = (SoapObject)envelope.getResponse();

                for (int i = 0; i < response.getPropertyCount(); i++) {
                    Object property = response.getProperty(i);
                    if (property instanceof SoapObject) {

                        ArrayList<SearchResults> results = new ArrayList<SearchResults>();
                         SearchResults sr1 = new SearchResults();

                      SoapObject info = (SoapObject) property;
                      String description = info.getProperty("description").toString();
                      String name = info.getProperty("name").toString();
                      String date = info.getProperty("date").toString(); 

                        Log.d("This is the list of id:",description);
                        Log.d("This is the list of name:",name);
                        Log.d("This is the list of date:",date);

                 sr1.setDescription(description);
                     sr1.setName(name);
                     sr1.setDate(date);                 
                     results.add(sr1);

                       final ListView lv1 = (ListView) findViewById(R.id.ListView01);
                       lv1.setAdapter(new MyCustomBaseAdapter(this, results));

                       lv1.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                         Object object = lv1.getItemAtPosition(position);
                         SearchResults fullObject = (SearchResults)object;
                         Toast.makeText(second.this, "You have chosen: " + " " + fullObject.getName(), Toast.LENGTH_LONG).show();

                      //   return;
                        }  
                      });  
             }
           }
         }

Solution

  • Its because you are creating a new ArrayList everytime inside a for loop, so its over-writing the last data inside the ArrayList created.

    It should be like,

    ArrayList<SearchResults> results = new ArrayList<SearchResults>();
    for (int i = 0; i < response.getPropertyCount(); i++) {
      SearchResults sr1 = new SearchResults();
      // fetch results
    } 
    // update the List
    ListView lv1 = (ListView) findViewById(R.id.ListView01);   
    lv1.setAdapter(new MyCustomBaseAdapter(this, results));