Android fragments 类extend fragement或asynchtask中的getapplication()

Android fragments 类extend fragement或asynchtask中的getapplication(),android-fragments,android,Android Fragments,Android,在ExtendeFragement类中没有getapplication(),在asynchtask中,我只有如何获取应用程序的上下文 因为我需要调用全局变量 GlobalVariables appState = (GlobalVariables) getApplication(); Encounter EncounterObject = appState.encounters.get(position); 在这两个班级中的任何一个 ackage com.appnetics; import

在ExtendeFragement类中没有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);
    }
ackage com.appnetics;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.StatusLine;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入android.app.Application;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.os.AsyncTask;
/*
*onPreExecute()-创建进度条对话框。
*doInBackground()-启动新线程。
*onPostExecute()-关闭进度条对话框。
* */
类遇到BackgroundWorker扩展AsyncTask{
ProgressDialog连接ProgressDialog;
私人语境;
公共遭遇BackgroundWorker(上下文)
{
this.context=context;
}
@凌驾
受保护的void onPreExecute(){
connectionProgressDialog=新建ProgressDialog(上下文);
connectionProgressDialog.setCancelable(false);
connectionProgressDialog.setCanceledOnTouchOutside(false);
connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_微调器);
connectionProgressDialog.setMessage(“上传线索…”);
connectionProgressDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
//调用Conference web服务
字符串结果=null;
StringBuilder URL=新的StringBuilder();
URL.append(“http://163.121.237.103/ali“”;//this.URL.getText()
append(“/Service.svc/returnMeeting”);
/*URL.append(“http://192.168.254.31:90/Service.svc/Login/");
append(username.getText());
URL.append(“/”);
append(password.getText());
finalResult.setText(URL.toString())*/
///////////////////////////// 
HttpClient=new DefaultHttpClient();
HttpGet HttpGet=新HttpGet(
URL.toString());
试一试{
HttpResponse response=client.execute(httpGet);
StatusLine StatusLine=response.getStatusLine();
int statusCode=statusLine.getStatusCode();
如果(状态代码==200){
HttpEntity=response.getEntity();
InputStream内容=entity.getContent();
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(内容));
//阅读JSON
结果=reader.readLine();
//将字符串转换为数组
GlobalVariables appState=新的GlobalVariables();
appState.Conferences=新的ArrayList();
JSONArray数组=新的JSONArray(结果);
//循环并填充
对于(int i=0;i使用:


我需要应用程序对象而不是上下文对象我需要说GlobalVariables appState=(GlobalVariables)getApplication();如果我在扩展ArrayAdapter类中需要它怎么办您可以将getActivity().getApplication()作为参数传递给ArrayAdapter类构造函数:)检查此示例
getActivity().getApplication();