用于数据库构造函数的Android getBaseContext()

时间:2013-04-03 14:25:31

标签: android

我的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;
    
    
            }
    
    
            } 
    
    
     }
    
  3. 如何从databseAdapter类创建对象?

1 个答案:

答案 0 :(得分:4)

  

如何从databseAdapter类创建对象?

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

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

然后使用:

DatabaseAdapter da = new DatabaseAdapter(mContext);