0

我有 2 个单独的类供我的 android 应用程序将数据插入到 sqllite 数据库

  1. 带有此构造函数的 DataBaseAdapterClass

    public DatabaseAdapter(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    
    }
    
  2. 我正在使用另一个名为 webservise 的类从 Web 服务获取数据并插入到 sqllite 数据库中,因为我使用了这个 Asyntask 类。

    public class FoodCityWebService {
    
        String myValue = "";
        private final String NAMESPACE="http://tempuri.org/";
        private final String URL = "xxxxxx:xxxx/Service1.asmx?WSDL";
    
        public FoodCityWebService(){
    
        }
    
        public FoodCityWebService(String param1){
    
        }
    
        public String getValueFromService()
        {
            myValue =  new web().execute("df").toString();
            return myValue;
        }
    
    
    
        public class web extends AsyncTask<String,Integer, String>
        { 
              String result = "";
              final String SOAP_ACTION="http://tempuri.org/GetUserName";
            @Override
            protected String doInBackground(String... params) {
    
                final String METHOD_NAME="GetUserName";
    
                SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
                request.addProperty("name","Bread");
                SoapSerializationEnvelope envelop=new SoapSerializationEnvelope(SoapEnvelope.VER11);//ver11 version
                envelop.dotNet=true;//only for dotnet       
                envelop.setOutputSoapObject(request);
    
                 HttpTransportSE httptransportse=new HttpTransportSE(URL);
                 String x ="";
                try{
                httptransportse.call(SOAP_ACTION, envelop);
    
                SoapPrimitive response=(SoapPrimitive)envelop.getResponse();
    
    
    
                    x = response.toString();
    
                    DatabaseAdapter da = new DatabaseAdapter(); // *ERROR!!!*           
                    da.addItem(new Item("CityItem1","Bread",Double.parseDouble(x),"222"));
    
    
    
                }catch (Exception e) {
    
                    e.printStackTrace();
    
                }
    
                return x;
    
    
            }
    
    
            } 
    
    
     }
    

如何从 databseAdapter 类创建对象?

4

1 回答 1

4

如何从 databseAdapter 类创建对象?

一种基本方法是将 Context 添加到构造函数的参数中并将其保存为成员变量:

private Context mContext;
public FoodCityWebService(Context context){
    mContext = context;
}

然后使用:

DatabaseAdapter da = new DatabaseAdapter(mContext); 
于 2013-04-03T14:31:55.010 回答