私はアンドロイドの新人です。私はアンドロイドのアプリを持っています。このデータを返した後、.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;
}
}
}
誰かが私を助けることができます.Thanks