类中没有 getapplication() 扩展片段和 asynchtask ,我只有上下文如何获取应用程序
因为我需要调用全局变量
GlobalVariables appState = (GlobalVariables) getApplication();
Encounter EncounterObject = appState.encounters.get(position);
在这些课程中的任何一个
ackage com.appnetics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
/*
* onPreExecute()- create the progress bar dialog.
* doInBackground()- start the new thread.
* onPostExecute()- dismiss the progress bar dialog.
* */
class EncounterBackgroundWorker extends AsyncTask<Void, Void, Void> {
ProgressDialog connectionProgressDialog;
private Context context;
public EncounterBackgroundWorker(Context Context )
{
this.context=Context;
}
@Override
protected void onPreExecute() {
connectionProgressDialog = new ProgressDialog(context);
connectionProgressDialog.setCancelable(false);
connectionProgressDialog.setCanceledOnTouchOutside(false);
connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
connectionProgressDialog.setMessage("Uploading Leads...");
connectionProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Call the Encounter web service
String Result = null;
StringBuilder URL = new StringBuilder();
URL.append("http://163.121.237.103/ali");//this.URL.getText()
URL.append("/Service.svc/ReturnEncounter");
/*URL.append("http://192.168.254.31:90/Service.svc/Login/");
URL.append(username.getText());
URL.append("/");
URL.append(password.getText());
finalResult.setText( URL.toString() ) ;*/
/////////////////////////////
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
URL.toString() );
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
// Read the JSON
Result = reader.readLine();
// Convert the string to Array
GlobalVariables appState = new GlobalVariables();
appState.encounters =new ArrayList<Encounter>();
JSONArray array=new JSONArray(Result);
// Loop through and fill
for(int i=0;i<array.length();i++){
JSONObject elem=(JSONObject)array.get(i);
Encounter encounter=new Encounter();
encounter.PatientID=elem.getInt("PatientID");
encounter.PatientName = elem.getString("PatientName");
encounter.NOKName= elem.getString("NOKName");
encounter.ReferalName= elem.getString("ReferalName");
encounter.bookingDate= elem.getString("bookingDate");
encounter.Onclogist= elem.getString("Onclogist");
encounter.EncounterStatus= elem.getString("EncounterStatus");
encounter.EncounterType= elem.getString("EncounterType");
encounter.Regieme= elem.getString("Regieme");
appState.encounters.add(encounter);
}
} else {
Result = "error";
}
} catch (ClientProtocolException e) {
Result = "error";
e.printStackTrace();
} catch (IOException e) {
Result = "error";
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
connectionProgressDialog.dismiss();
}
}
---------------- and I call it in --------------
/*
* This class to provide the code for the Chemo unit screen that will be used
*
* */
package com.appnetics;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class ChemoUnit extends Fragment {
/*The main controls*/
TextView Patient, Nok , Referrer , Oncologist = null ;
ListView MainGrid ;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(
R.layout.chemounit,
container,
false);
// Intialize the Controls
Patient = ((TextView) view.findViewById(R.id.Patient));
Nok = ((TextView) view.findViewById(R.id.Nok));
Referrer = ((TextView) view.findViewById(R.id.Referrer));
Oncologist = ((TextView) view.findViewById(R.id.Oncologist));
//Intialize the record Grid
LinearLayout formLayout = (LinearLayout)view.findViewById(R.id.ChemoUnitGrid);
formLayout.removeAllViews();
MainGrid = new ListView(getActivity().getApplicationContext());
MainGrid.setVisibility(ListView.VISIBLE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
MainGrid.setLayoutParams(params);
//1. set the header
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.chemounitgridheader, MainGrid, false);
MainGrid.addHeaderView(header, null, false);
//2. Call the web Service
new EncounterBackgroundWorker( getActivity() ).execute();
MainGrid.setAdapter(new Encounteradapter(view.getContext(), null));
// Finally add it
formLayout.addView(MainGrid);
// Return the Result
return inflater.inflate(R.layout.chemounit, container, false);
}