2013-02-04 38 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

是数组列表是在循环运行时创建的。现在我改变了.. –