views:

1153

answers:

2

I'm working on an app that calls a web service, then the webservice returns an array list. My problem is I am having trouble getting the data into the ArrayList and then displaying in a ListView. Any ideas what I am doing wrong? I know for a fact the web service returns an ArrayList. Everything seems to be working fine, just no data in the ListView or the ArrayList.....Thanks in advance!

EDIT: It donned on me that the data the webservice responds with is a complex type. I really think that's where I am getting hung up, but not sure how to correct it.

APRIL 21, 2010:

So I finally figured out my webservice returns a JSON Array. Now getting that over to the ArrayList and/or ListView is the challenge. I have a feeling that once this project is done, it might make for a great tutorial or example...

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

Sample result data from webservice:

<ArrayOfArtists>
    <Artist>
        <Track>.......</Track>
    </Artist>
    <Artist>
        <Track>.......</Track>
    </Artist>
    <Artist>
        <Track>.......</Track>
    </Artist>
</ArrayOfArtists>
A: 

You should set the adapter in onCreate and in handleMessage just call lv.getAdapter().notifyDataSetChanged()

BrennaSoft
Thanks Rpond, I can see how that would make things a little easier. However I'm still stuck. The code is triggering the if(Artist_Result.isEmpty()) block. I have to be messing up somwhere between the soap object and the ArrayList...
Maskau
A: 

You could try looking at this tutorial for ksoap, android and .NET

DFDF