0

配列 SOAP アクションの結果をリストビューに表示しようとしています。アイテムを取得して文字列として保存することはできましたが、すべての文字列をリストビューに入れることはできません。私は間違って何をしていますか?? 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;
                        }  
                      });  
             }
           }
         }
4

2 に答える 2

1

ArrayListこれは、内で毎回新しいを作成しているためです。そのため、作成されたfor loop内の最後のデータが上書き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));
于 2013-02-04T11:41:12.113 に答える
0

これを試してください: コードを変更しました。リストビュー アダプターが結果をループ外にバインドするためです。

  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;
                    }  
                  });  
         }
于 2013-02-04T11:52:57.973 に答える