4

我正在开发一个调用 Web 服务的应用程序,然后 Web 服务返回一个数组列表。我的问题是我无法将数据放入 ArrayList,然后在 ListView 中显示。任何想法我做错了什么?我知道 Web 服务返回一个 ArrayList 的事实。一切似乎都运行良好,只是 ListView 或 ArrayList 中没有数据.....提前致谢!

编辑:我认为网络服务响应的数据是一种复杂类型。我真的认为这就是我被挂断的地方,但不知道如何纠正它。

2010 年 4 月 21 日:

所以我终于发现我的网络服务返回了一个 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>
4

1 回答 1

1

您应该在 onCreate 和 handleMessage 中设置适配器,只需调用 lv.getAdapter().notifyDataSetChanged()

于 2010-04-13T13:46:03.980 回答