什么是扩展按钮类中的Context Means?什么是上下文的默认值?

时间:2017-05-11 12:45:00

标签: java android class android-context extended-events

这是我的扩展按钮类`

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

扩展按钮类constructure参数中的Context是什么?

我想将这个类用作其他类中的数组,但我不确定我在Context中写的是什么?我想从肥皂服务中获取酒店按钮并使用下面的代码。此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]);
        }
    }
}

1 个答案:

答案 0 :(得分: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;
        }
    }

}

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