2010-04-13 117 views
4

我正在調用一個調用Web服務的應用程序,然後webservice返回一個數組列表。我的問題是我無法將數據放入ArrayList中,然後在ListView中顯示。任何想法我做錯了什麼?我知道Web服務返回一個ArrayList。一切似乎工作正常,只是沒有數據在ListView或ArrayList .....在此先感謝!將ksoap對象綁定到Android中的ArrayList時遇到困難

編輯:它穿上我,web服務響應的數據是一個複雜的類型。我真的認爲這是我掛斷電話的地方,但不知道如何糾正。

2010年4月21日:

所以我終於想通了,我的web服務返回JSON陣列。現在把它交給ArrayList和/或ListView是挑戰。我有一次這個項目做了的感覺,它可能使一個偉大的教程或例子...

package com.maskau; 

import java.util.ArrayList; 
import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.AndroidHttpTransport; 
import android.app.*; 
import android.os.*; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.view.View; 
import android.view.View.OnClickListener; 


public class Home extends Activity implements Runnable{ 

/** Called when the activity is first created. */ 
public static final String SOAP_ACTION = "http://bb.mcrcog.com/GetArtist"; 
public static final String METHOD_NAME = "GetArtist"; 
public static final String NAMESPACE = "http://bb.mcrcog.com/"; 
public static final String URL = "http://bb.mcrcog.com/karaoke/service.asmx"; 
String wt; 
public static ProgressDialog pd; 
TextView text1; 
ListView lv; 
static EditText myEditText; 
static Button but; 
private ArrayList<String> Artist_Result = new ArrayList<String>(); 



@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 
    myEditText = (EditText)findViewById(R.id.myEditText); 
    text1 = (TextView)findViewById(R.id.text1); 
    lv = (ListView)findViewById(R.id.lv); 
    but = (Button)findViewById(R.id.but); 

    but.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      wt = ("Searching for " + myEditText.getText().toString()); 
      text1.setText(""); 
      pd = ProgressDialog.show(Home.this, "Working...", wt , true, false); 
      Thread thread = new Thread(Home.this); 
      thread.start(); 
     } 
    } 
    ); 
} 

public void run() 
{ 
     try 
    { 
      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
      PropertyInfo pi = new PropertyInfo(); 
      pi.setName("ArtistQuery"); 
      pi.setValue(Home.myEditText.getText().toString()); 
      request.addProperty(pi); 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.dotNet = true; 
      envelope.setOutputSoapObject(request); 

      AndroidHttpTransport at = new AndroidHttpTransport(URL); 
      at.call(SOAP_ACTION, envelope); 
      java.util.Vector<Object> rs = (java.util.Vector<Object>)envelope.getResponse(); 
      if (rs != null) 
      { 
       for (Object cs : rs) 
       { 
        Artist_Result.add(cs.toString()); 
       } 
      } 
     } 

    catch (Exception e) 
     { 
     // Added this line, throws "org.ksoap2.serialization.SoapObject" when run 
      Artist_Result.add(e.getMessage());  
     } 

    handler.sendEmptyMessage(0); 
} 
private Handler handler = new Handler() 
{ 
    @Override 
    public void handleMessage(Message msg) 
    { 
     ArrayAdapter<String> aa; 
    aa = new ArrayAdapter<String>(Home.this, android.R.layout.simple_list_item_1, Artist_Result); 
    lv.setAdapter(aa); 
    try 
    { 
     if (Artist_Result.isEmpty()) 
     { 
       text1.setText("No Results"); 
     } 
     else 
     { 

       text1.setText("Complete"); 
       myEditText.setText("Search Artist"); 
     } 
    } 
    catch(Exception e) 
    { 
    text1.setText(e.getMessage()); 
    } 
    aa.notifyDataSetChanged(); 
    pd.dismiss(); 
    } 
}; 
} 

從Web服務樣本結果數據:

<ArrayOfArtists> 
    <Artist> 
     <Track>.......</Track> 
    </Artist> 
    <Artist> 
     <Track>.......</Track> 
    </Artist> 
    <Artist> 
     <Track>.......</Track> 
    </Artist> 
</ArrayOfArtists> 
+0

你可以試試看[ksoap,android和.NET教程](http://seesharpgears.blogspot.com/2010/10/ksoap- android-web-service-tutorial-with.html) – DFDF 2010-10-25 14:58:12

回答

1

則應該在的onCreate適配器並在handleMessage中調用lv.getAdapter()。notifyDataSetChanged()

+0

謝謝Rpond,我可以看到這將如何使th方便一點。但我仍然卡住了。代碼觸發if(Artist_Result.isEmpty())塊。我必須弄亂肥皂對象和ArrayList之間的某處... – Maskau 2010-04-13 14:08:53