我是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;
}
}
我的Activity类代码是:
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;
}
}
}
有人可以帮帮我。谢谢
答案 0 :(得分: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();
}
}
}
BTW StableArrayAdapter
未在您的代码中使用。 (我在共享代码中没有看到任何对此的引用。)如果它不相关,请从此处删除它。
答案 1 :(得分:0)
您正在UI线程上执行所有操作。寻找AsyncTask或Threads。这可能会导致一个没有响应的窗口,你仍然可以测试它,如果你按下等待但它不正确,只能在编写AsyncTask或Thread之前将其用于测试。
StableArrayAdapter在任何地方都没有使用;更好地清理你的代码!
您可以在解析后记录或调试list
的属性是否有任何数据;
上面提到的MyArrayAdapter代码似乎很好。