我想知道为什么静态内部的代码可以查看列表中的第1项等数据,但是当我放入名为onPostExecute()的asynctask函数时,它不能。此页面是android默认布局,用于查看listview中的项目数据,并且可以查看旁边的详细信息
package com.example.mobile_e_commerce.dummy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
import com.example.mobile_e_commerce.R;
/**
* Helper class for providing sample content for user interfaces created by
* Android template wizards.
* <p>
* TODO: Replace all uses of this class before publishing your app.
*/
public class DummyContent {
/**
* An array of sample (dummy) items.
*/
private static final ResourceBundle rb = ResourceBundle.getBundle("com.example.mobile_e_commerce.webserviceurl");
private final String NAMESPACE = rb.getString("WSDLTargetNamespace");
private final String SoapURL = rb.getString("SoapAddress");
private final String SOAP_ACTION = rb.getString("SoapAction");
private final String METHOD_NAME = rb.getString("OperationName");
private final String NAMESPACE2 = rb.getString("WSDLTargetNamespace");
private final String SoapURL2 = rb.getString("SoapAddress");
private final String SOAP_ACTION2 = rb.getString("SoapAction2");
private final String METHOD_NAME2 = rb.getString("OperationName2");
private final String NAMESPACE3 = rb.getString("WSDLTargetNamespace");
private final String SoapURL3 = rb.getString("SoapAddress");
private final String SOAP_ACTION3 = rb.getString("SoapAction3");
private final String METHOD_NAME3 = rb.getString("OperationName3");
private final String NAMESPACE4 = rb.getString("WSDLTargetNamespace");
private final String SoapURL4 = rb.getString("SoapAddress");
private final String SOAP_ACTION4 = rb.getString("SoapAction4");
private final String METHOD_NAME4 = rb.getString("OperationName4");
private int number = 0;
private String myNumber = "";
public static List<DummyItem> ITEMS = new ArrayList<DummyItem>();
/**
* A map of sample (dummy) items, by ID.
*/
class NamedataLoader extends AsyncTask<String,String,String>{
//ProductCollection pc = new ProductCollection();
//TextView textView = new TextView(pc);
private String valuePass;
PropertyInfo getInfo = new PropertyInfo();
@Override
protected String doInBackground(String... arg0) {
SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
request.addProperty("number", number);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SoapURL);
Object response = null;
try
{
httpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
valuePass = response.toString();//response.toString();//trying to get from my
//webservices but url not found,solve it later
}
catch (Exception exception)
{
exception.printStackTrace();
Log.e("Apps Error", exception.toString());
}
return valuePass;
}
@Override
protected void onPostExecute(String tV) {
try{
// TextView textView = (TextView)findViewById(R.id.Product1);
// textView.setText(tV.toString());
我在这里添加addItem函数,以便它可以在当前页面中加载项目作为后台任务
myNumber = Integer.toString(number);
addItem(new DummyItem(myNumber,tV.toString()));
}
catch(Exception ee)
{
Log.e("App onDataLoader Error: ", ee.toString());
}
}
}
public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
//The code here after comment can add to the listview//
//static {
// Add 3 sample items.
// addItem(new DummyItem("1", "Item 1"));
//addItem(new DummyItem("2", "Item 2"));
// addItem(new DummyItem("3", "Item 3"));
//}
添加项目功能
private static void addItem(DummyItem item) {
ITEMS.add(item);
ITEM_MAP.put(item.id, item);
}
/**
* A dummy item representing a piece of content.
*/
public static class DummyItem {
public String id;
public String content;
public DummyItem(String id, String content) {
this.id = id;
this.content = content;
}
@Override
public String toString() {
return content;
}
}
}