2017-05-11 57 views
0

這是我的延伸按鈕class`上下文在擴展按鈕類中是什麼意思? Context的默認值是什麼?

import android.content.Context; 
import android.widget.Button; 

public class Hotel extends Button{ 
    String id; 
    String name; 
    String company_id; 

    public Hotel(Context context, String id, String name, String company_id) { 
     super(context); 
     this.id = id; 
     this.name = name; 
     this.company_id = company_id; 
    } 
} 

什麼是語境在擴展按鈕類建設淺析參數?

我想用這個類作爲另一個類中的數組,但我不知道我應該在上下文中寫什麼? İamtring從肥皂服務獲取酒店按鈕並使用下面的代碼。這個GetHotels方法在另一個類中。我只是對上下文事物感到困惑。請幫幫我。

import android.content.Context; 

import org.json.JSONArray; 
import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapPrimitive; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 

/** 
* Created by arzucaki on 09/05/2017. 
*/ 

public class ServiceManager { 
    private static final String METHOD_NAME = "GetHotels"; 
    private static final String NAMESPACE = "http://tempuri.org/"; 
    private static final String SOAP_ACTION = "http://tempuri.org/GetHotels"; 
    private static final String URL = "http://192.168.1.170/HotelApps_WebServices.asmx"; 
    public static Context mContext; 
    SoapObject soapObject; 
    SoapSerializationEnvelope soapSerializationEnvelope; 
    HttpTransportSE httpTransportSE; 

    public Hotel[] GetHotels(int Company_id) { 

     soapObject = new SoapObject(NAMESPACE, METHOD_NAME); 
     soapObject.addProperty("Company_id", Company_id); 

     soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     soapSerializationEnvelope.dotNet = true; 
     soapSerializationEnvelope.setOutputSoapObject(soapObject); 

     httpTransportSE = new HttpTransportSE(URL); 
     httpTransportSE.debug = true; 

     Hotel hotels[]=new Hotel[6]; 
     try { 
      httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope); 
      SoapPrimitive soapPrimitive=(SoapPrimitive)soapSerializationEnvelope.getResponse(); 
      JSONArray jsonHotels = new JSONArray(soapPrimitive.toString()); 

      for (int i=0; i<6; i++) { 
       hotels[i]=new Hotel(mContext, jsonHotels.getJSONObject(i).getString("Hotel_id"),jsonHotels.getJSONObject(i).getString("Hotel_Name"), jsonHotels.getJSONObject(i).getString("Company_id")); 

      } 
      return hotels; 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
      return hotels; 
     } 
    } 

} 

我打電話的方法就在這裏

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.GridLayout; 

public class Hotels extends AppCompatActivity { 
    ServiceManager service=new ServiceManager();; 
    Hotel hotels[]; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_hotels); 
     GridLayout glHotels=(GridLayout) findViewById(R.id.glHotels); 

     Thread getHotels=new Thread(new Runnable() { 
      @Override 
      public void run() { 
       hotels=service.GetHotels(1); 
      } 
     }); 
     getHotels.start(); 
     for (int i=0; i<hotels.length; i++) { 
      glHotels.addView(hotels[i]); 
     } 
    } 
} 
+0

pass getApplica tionContext()作爲上下文中的參數 –

+0

你是什麼意思? – arzucaki

+0

我的意思是:酒店酒店=新酒店(getApplicationContext(),......) –

回答

0

變化烏爾的ServiceManager類的方法簽名是這樣的:

public class ServiceManager { 
    private static final String METHOD_NAME = "GetHotels"; 
    private static final String NAMESPACE = "http://tempuri.org/"; 
    private static final String SOAP_ACTION = "http://tempuri.org/GetHotels"; 
    private static final String URL = "http://192.168.1.170/HotelApps_WebServices.asmx"; 
    public static Context mContext; 
    SoapObject soapObject; 
    SoapSerializationEnvelope soapSerializationEnvelope; 
    HttpTransportSE httpTransportSE; 
public ServiceManager(Context context){ 
    this.mContext = context;//now u can pass mContext to Hotel Constructor 
} 
    public Hotel[] GetHotels(int Company_id) { 

     soapObject = new SoapObject(NAMESPACE, METHOD_NAME); 
     soapObject.addProperty("Company_id", Company_id); 

     soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     soapSerializationEnvelope.dotNet = true; 
     soapSerializationEnvelope.setOutputSoapObject(soapObject); 

     httpTransportSE = new HttpTransportSE(URL); 
     httpTransportSE.debug = true; 

     Hotel hotels[]=new Hotel[6]; 
     try { 
      httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope); 
      SoapPrimitive soapPrimitive=(SoapPrimitive)soapSerializationEnvelope.getResponse(); 
      JSONArray jsonHotels = new JSONArray(soapPrimitive.toString()); 

      for (int i=0; i<6; i++) { 
       hotels[i]=new Hotel(mContext, jsonHotels.getJSONObject(i).getString("Hotel_id"),jsonHotels.getJSONObject(i).getString("Hotel_Name"), jsonHotels.getJSONObject(i).getString("Company_id")); 

      } 
      return hotels; 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
      return hotels; 
     } 
    } 

} 

在類似這樣的活動課初始化服務經理:

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.GridLayout; 

public class Hotels extends AppCompatActivity { 
    ServiceManager service=new ServiceManager(getApplicationContext()); 
    Hotel hotels[]; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_hotels); 
     GridLayout glHotels=(GridLayout) findViewById(R.id.glHotels); 

     Thread getHotels=new Thread(new Runnable() { 
      @Override 
      public void run() { 
       hotels=service.GetHotels(1); 
      } 
     }); 
     getHotels.start(); 
     for (int i=0; i<hotels.length; i++) { 
      glHotels.addView(hotels[i]); 
     } 
    } 
} 
+0

錯誤:(8,28)錯誤:構造函數ServiceManager類中的ServiceManager無法應用於給定的類型; 必需:上下文 找到:沒有參數 原因:實際和正式參數列表的長度不同 – arzucaki

+0

檢查更新的活動類,並作出相應的更改 –

+0

它給運行時錯誤「應用程序已停止」,但我在調試模式控制數組它沒問題。應用程序停止,但它將酒店作爲數組返回。 – arzucaki