【问题标题】:Display String arrayfrom Soap Webservice to listview Android从 Soap Web 服务显示字符串数组到 listview Android
【发布时间】:2013-02-04 11:36:16
【问题描述】:

我试图将我的数组soap 操作结果显示到listview。我设法检索项目并将其存储为字符串,但是,我似乎无法将所有字符串放入列表视图中。我在做什么错??当我 Log.d 时,它会以正确的顺序显示所有内容(这就是我想要的)。但是,当我将它添加到我的 SearchResults 时,它只显示最后的详细信息。

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

【问题讨论】:

    标签: android arrays android-listview ksoap2


    【解决方案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));
    

    【讨论】:

      【解决方案2】:

      试试这个: 我已经更改了代码。因为 listview 适配器将结果绑定到循环之外。

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

      【讨论】:

      • 您的代码错误。我什至可以显示任何东西,使用你的代码。请查看我在上面打勾的正确答案.. 感谢您的努力~
      猜你喜欢
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 2013-11-21
      • 1970-01-01
      • 2012-05-21
      相关资源
      最近更新 更多