扩展按钮 class 中的上下文含义是什么?上下文的默认值是什么?

What is Context Means in extended button class? What is default of a Context?

这是我的扩展按钮class`

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;
    }
}

什么是扩展按钮中的上下文 class 构造参数?

我想将此 class 用作另一个 class 中的数组,但我不确定我应该在上下文中写什么?我正在尝试从 soap 服务获取酒店按钮并使用下面的代码。此 GetHotels 方法在另一个 class 中。我只是对 Context 的事情感到困惑。请帮助我。

 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]);
        }
    }
}

像这样更改您的 ServiceManager Class 的方法签名:

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;
        }
    }

}

在Activity Class中初始化服务管理器如下:

 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]);
        }
    }
}