This is my extended button 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;
}
}
What is Context in an extended button class constructure parameter?
I want to use this class as an array in an other class but i am not sure what sould i write in Context place? İam tring to get hotel buttons from soap service and using this codes below. This GetHotels method is in an other class. I am just confused about that Context thing. Help me please.
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;
}
}
}
I am calling the method right here
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]);
}
}
}
Change the method signature of ur ServiceManager Class like this:
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;
}
}
}
In the Activity Class initialize service manager like this:
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]);
}
}
}