-1

我是android的新手。我在android中有一个应用程序,在返回这些数据后,我从.Net WebService(名称和ID)返回一些数据,我使用适配器在列表视图中设置所有这些名称。我已经用调试器检查了 ARRAY LIST 正在从 WEB SERVICE 获取数据,但是 android SDK 中的 LISTVIEW 没有显示任何结果。

我的适配器代码是:

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<MyValues>{
Context context; 
int layoutResourceId;    
List data = null;

public MyArrayAdapter(Context context, int layoutResourceId, List data) {
       super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder view;

        if(rowView == null)
        {
            // Get a new instance of the row layout view
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            rowView = inflater.inflate(R.layout.multirow, null);

            // Hold the view objects in an object, that way the don't need to be "re-  finded"
            view = new ViewHolder();
            view.name= (TextView) rowView.findViewById(R.id.rowTextView);
            view.address= (TextView) rowView.findViewById(R.id.secondLine);

            rowView.setTag(view);
        } else {
            view = (ViewHolder) rowView.getTag();
        }

        /** Set data to your Views. */
        MyValues item =(MyValues) data.get(position); //List.get(position);
        view.name.setText(item.getName());
        view.address.setText(item.getAddress());

        return rowView;
    }

    protected static class ViewHolder{
        protected TextView name;
        protected TextView address;
    }


}

我的价值观课程代码是:

public class MyValues {
private String name;
private String address;

public MyValues(String name, String address) {
    this.name = name;
    this.address = address;
}
public void setName(String name) {
    this.name= name;
}
public String getName() {
    return name;
}
public void setAddress(String address) {
    this.address= address;
}
public String getAddress() {
    return address;
}
}

我的活动类代码是:

 import java.util.ArrayList;

 import java.util.HashMap;
 import java.util.List;
 import android.app.Activity;
 import android.content.Context;
 import android.os.Bundle;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;


import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class SimpleListViewActivity extends Activity {

private static String SOAP_ACTION_D = "http://tempuri.org/GetSalesMen";
private static String NAMESPACE = "http://tempuri.org/";
 private static String METHOD_NAME_D = "GetSalesMen";
private static String URL_D = "http://apollon.dnet.gr/webservice/Service1.asmx";
 /** Called when the activity is first created. */
 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

SoapObject table = null;                
SoapObject tableRow = null;     
SoapObject responseBody = null;    

 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_D);        

SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);
envelope.dotNet = true;

try {
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_D);

    List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
    headerList.add(new HeaderProperty("Authorization", "Basic " +   org.kobjects.base64.Base64.encode("administrator:MyPassword".getBytes())));

    androidHttpTransport.call(SOAP_ACTION_D, envelope, headerList);

    SoapObject result = (SoapObject)envelope.bodyIn;
    int intPropertyCount = result.getPropertyCount();
    {
        responseBody = (SoapObject) envelope.getResponse();

        responseBody = (SoapObject) responseBody.getProperty(1);

        table = (SoapObject) responseBody.getProperty(0);
        ArrayList<MyValues> list = new ArrayList<MyValues>();
        SoapObject responseChild = (SoapObject) result.getProperty(0);
        int lengthOfResponseChild = responseChild.getPropertyCount();

        for (int j = 0; j < lengthOfResponseChild; j++){
            tableRow  = (SoapObject) table.getProperty(j);
            list.add(new MyValues(tableRow.getProperty(2).toString(),      tableRow.getProperty(1).toString()));

        }  
        ListView lv = (ListView) findViewById(R.id.mainListView);
        MyArrayAdapter adapter = new MyArrayAdapter(this,R.id.mainListView, list);
        lv.setAdapter(adapter);

    }
} catch (Exception e) {
    e.printStackTrace();
}

 }

    public class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
        List<String> objects) {
      super(context, textViewResourceId, objects);
      for (int i = 0; i < objects.size(); ++i) {
        mIdMap.put(objects.get(i), i);
      }
    }

    @Override
    public long getItemId(int position) {
      String item = getItem(position);
      return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }

  }

}

有人可以帮我吗。谢谢

4

2 回答 2

0

getCount在您的适配器类中实现方法:

public class MyArrayAdapter extends ArrayAdapter<MyValues>{
   //...Your current codes here
   @Override
   public int getCount() {
       if(data == null) {
          return 0;
       } else { 
          return data.size();
       }
   }
}

顺便说一句StableArrayAdapter,您的代码中没有使用。(我在您的共享代码中没有看到任何对它的引用。)如果它不相关,请从此处删除它。

于 2013-11-13T18:32:41.930 回答
0

您正在 UI 线程上做所有事情。寻找 AsyncTask 或 Threads。这可能会导致一个没有响应的窗口,如果您按下等待,您仍然可以测试它,但它不正确,仅在编写 AsyncTask 或线程之前将其用于测试。

StableArrayAdapter 没有在任何地方使用是对的;更好地清理你的代码!

list解析后是否可以记录或调试具有任何数据的属性;

MyArrayAdapter 提到的代码似乎很好。

于 2013-11-13T18:36:40.937 回答