2013-02-04 76 views
0

我試圖顯示我的數組肥皂行動結果到列表視圖。我設法檢索項目並將其存儲爲字符串,但是,我似乎無法將所有字符串放入列表視圖中。我在做錯什麼?當我Log.d時,它以正確的順序顯示所有內容(這是我想要的)。但是,當我將它添加到我的SearchResults時,它只顯示最後的細節。顯示字符串數組從肥皂Web服務到列表視圖Android

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

回答

1

它,因爲你正在創建一個for loop內新ArrayList每次,所以其對寫作產生的ArrayList內的最後一個數據。

應該是這樣,

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)); 
+0

Thanks @BomberMan !! –

0

試試這個: 我修改了code.Because列表視圖適配器綁定外循環的結果。

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


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

你的代碼是錯誤的。我甚至可以使用你的代碼顯示任何東西。請檢查我上面勾選的正確答案。謝謝你的努力,雖然〜 –

+0

是數組列表是在循環運行時創建的。現在我改變了.. –