Android 异步任务获取活动

Android 异步任务获取活动,android,android-asynctask,android-toolbar,Android,Android Asynctask,Android Toolbar,我正在尝试从异步任务设置工具栏标题。我在此行中得到一个无法解析的getActivity()方法: ((ActionBarActivity)getActivity()).getSupportActionBar().setTitle(name); 我的代码如下所示: /** * Created by Mike on 2/28/15. */ import android.app.Activity; import android.app.Dialog; import android

我正在尝试从异步任务设置工具栏标题。我在此行中得到一个无法解析的getActivity()方法:

        ((ActionBarActivity)getActivity()).getSupportActionBar().setTitle(name);
我的代码如下所示:

/**
 * Created by Mike on 2/28/15.
 */
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Mike on 4/26/14.
 */
public class GetBeerDataJSON2 extends AsyncTask<String, Void, String> {

    Context c;
    String id;
    private ProgressDialog Dialog;


    public GetBeerDataJSON2 (Context context, String beerID)
    {
        c = context;
        id = beerID;
        Dialog = new ProgressDialog(c);
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return readJSONFeed(arg0[0]);
    }

    protected void onPreExecute() {
        Dialog.setMessage("Getting beer information");

        Dialog.setTitle("Loading");
        Dialog.show();
    }

    protected void onPostExecute(String result){
        //decode json here
        try{

            JSONObject o = new JSONObject(result);

            //get beer details



            //todo: lets get things from our new JSON

            String name = o.getString("name");
            String beerDescription = o.getString("description");
            String abv = o.getString("abv");
            String ibu = o.getString("ibu");
            String image = o.getString("icon");
            String glass = o.getString("glass");
            String beerBreweryStyle = o.getString("style");
            String beerBreweryName = o.getString("brewery");
            String status = o.getString("status");
            String rating = o.getString("rating");
            String food = o.getString("food");




            int beerRate = 0;
            beerRate = o.getInt("userRating");



            //prepare buttons
            //Button buttonBrewery = (Button) ((Activity) c).findViewById(R.id.buttonBrewery);
            //Button buttonStyle = (Button) ((Activity) c).findViewById(R.id.buttonStyle);

            TextView tv_breweryName = (TextView) ((Activity) c).findViewById(R.id.beerBreweryName);
            TextView tv_styleName = (TextView) ((Activity) c).findViewById(R.id.beerStyleName);

            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("styleName",beerBreweryStyle);
            editor.putString("beerName",name);
            editor.putString("lastBeer",name);
            editor.putString("breweryName",beerBreweryName);
            editor.putString("styleName",beerBreweryStyle);
            editor.commit();

            ((ActionBarActivity)getActivity()).getSupportActionBar().setTitle(name);


            //create text views
            TextView styleTitle = (TextView) ((Activity) c).findViewById(R.id.beerTitle);
            styleTitle.setText(name);

            TextView textABV = (TextView) ((Activity) c).findViewById(R.id.abv);
            textABV.setText(abv);

            TextView textIBU = (TextView) ((Activity) c).findViewById(R.id.IBU);
            textIBU.setText(ibu);

            TextView textGlass = (TextView) ((Activity) c).findViewById(R.id.glass);
            textGlass.setText(glass);

            TextView textDescription= (TextView) ((Activity) c).findViewById(R.id.beerDescription);
            textDescription.setText(beerDescription);

            String breweryButton = "Brewery: ";
            String styleButton = "Style: ";

            //todo: add beer name to shared prefs

            tv_breweryName.setText(beerBreweryName);
            tv_styleName.setText(beerBreweryStyle);

            //get user id
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
            String userName = prefs.getString("userName", null);
            final String userID = prefs.getString("userID", null);

            //check if user has beer
            String url = "http://www.beerportfolio.com/app_checkBeer.php?";
            String userURLComp = "u=" + userID;
            final String beerID = "&b=" + id;

            url = url + userURLComp + beerID;
            //new CheckBeerJSON(c,id,userID).execute(url);

            //todo: add code for check beer here

            if(status.equals("no")){




                //clear loader image
                LinearLayout ll = (LinearLayout) (LinearLayout)((Activity) c).findViewById(R.id.addBeerLayout);
                ll.removeAllViews();

                //Add beer add button
                LayoutInflater mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                LinearLayout addButton = (LinearLayout)((Activity) c).findViewById(R.id.addBeerLayout);
                addButton.addView(mInflater.inflate(R.layout.addbeerbutton_layout, null));

                //add on click listener here
                Button bt4 = (Button)((Activity) c).findViewById(R.id.button1);
                bt4.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // do whatever stuff you wanna do here



                        //get beer details
                        String url2 = "http://www.beerportfolio.com/app_addBeer.php?";
                        String urlUserID = "u=" + userID;
                        String urlBeerID = "&bID=" + id;


                        Log.d("url", id);
                        //construct url for adding beer
                        url2 = url2 + urlUserID + urlBeerID;



                        Log.d("url", url2);

                        //execute async on url to add to brewery
                        new AddBeer(c).execute(url2);

                        //to do: change to start rater
                        LinearLayout ll = (LinearLayout) ((Activity) c).findViewById(R.id.addBeerLayout);
                        ll.removeAllViews();

                        //add rater

                        LayoutInflater inflater = (LayoutInflater)((Activity) c).getSystemService(((Activity) c).LAYOUT_INFLATER_SERVICE);
                        LinearLayout addButton = (LinearLayout)((Activity) c).findViewById(R.id.addBeerLayout);
                        addButton.addView(inflater.inflate(R.layout.addrate_layout, null));


                        //add listener to rate button todo
                        //add listener to bar
                        addListenerOnRatingBar(c);




                    }
                });


            }

            else{


                //clear loader image
                LinearLayout ll = (LinearLayout) (LinearLayout)((Activity) c).findViewById(R.id.addBeerLayout);
                ll.removeAllViews();

                //inflate star rater
                LayoutInflater mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                LinearLayout addButton = (LinearLayout)((Activity) c).findViewById(R.id.addBeerLayout);
                addButton.addView(mInflater.inflate(R.layout.addrate_layout, null));

                RatingBar r = (RatingBar) ((Activity) c).findViewById(R.id.beerRatingBar);

                //get user data


                //get beer rating with async task and update rate bar
                String url2 = "http://www.beerportfolio.com/app_getRating.php?";
                String userURLComp2 = "u=" + userID;
                String beerID2 = "&b=" + beerID;

                url = url + userURLComp2 + beerID2;



                //new GetUserRating(c,r).execute(url2);
                r.setRating(beerRate);

                //add listener to bar
                addListenerOnRatingBar(c);



            }

            //end code here

            //todo: get rate code goes here


            TextView tv1 = (TextView) ((Activity) c).findViewById(R.id.beerRating);

            tv1.setText(rating + " / 5");

            //end rate code


            String url2 = "http://beerportfolio.com/app_beerDetails2.php?u="+ userID + "&b=" + beerID;
            //new GetBeerRateJSON(c,id).execute(url2);

            ImageView im1 = (ImageView) ((Activity) c).findViewById(R.id.image);
            if(image.equals("N/A")){
                //set image as png
                im1.setImageResource( R.drawable.noimage);
            }

            else{
                ImageDownloadTask imageD = new ImageDownloadTask(im1);
                imageD.execute(image);
            }


            //tdo: add food paring

            if (food.equals("N/A")){


            }




            Dialog.dismiss();







        }
        catch(Exception e){

        }

    }

    private void addListenerOnRatingBar(Context view) {
        RatingBar ratingBar = (RatingBar) ((Activity) view).findViewById(R.id.beerRatingBar);

        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float rating,
                                        boolean fromUser) {

                //next async task to update online database
                float stars = ratingBar.getRating();

                //get user details
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
                String userID = prefs.getString("userID", null);

                //get beer id
                String beerID = id;

                //get rating
                String urlRate = "r=" + String.valueOf(ratingBar.getRating());
                String urlUserID = "&u=" + userID;
                String urlBeerID = "&b=" + beerID;

                //construct url
                String url2 = "http://www.beerportfolio.com/app_rateUpdate.php?";

                url2 = url2 + urlRate + urlUserID + urlBeerID;


                //async task to update rating in database
                new UpdateRating(c).execute(url2);





            }
        });
    }

    public String getName(JSONObject json){
        String holder;

        try{
            holder = json.getJSONObject("data").getString("name");

        } catch (JSONException e) {
            holder = "N/A";
        }

        return holder;

    }

    public String getABV(JSONObject json){
        String holder;

        try{
            holder = json.getJSONObject("data").getString("abv");

        } catch (JSONException e) {
            holder = "N/A";
        }

        return holder;

    }

    public String getIBU(JSONObject json){
        String holder;

        try{
            holder = json.getJSONObject("data").getString("ibu");

        } catch (JSONException e) {
            holder = "N/A";
        }

        return holder;

    }

    public String getImage(JSONObject json){
        String holder;

        try{
            holder = json.getJSONObject("data").getJSONObject("labels").getString("large");

        } catch (JSONException e) {
            holder = "N/A";
        }

        return holder;

    }

    public String getGlass(JSONObject json){
        String holder;

        try{
            holder = json.getJSONObject("data").getJSONObject("glass").getString("name");

        } catch (JSONException e) {
            holder = "N/A";
        }

        return holder;

    }

    public String getBreweryName(JSONObject json){
        String holder;

        try{
            holder = json.getJSONObject("data").getJSONArray("breweries").getJSONObject(0).getString("name");

        } catch (JSONException e) {
            holder = "N/A";
        }

        return holder;

    }

    public String getBreweryStyle(JSONObject json){
        String holder;

        try{
            holder = json.getJSONObject("data").getJSONObject("style").getString("name");

        } catch (JSONException e) {
            holder = "N/A";
        }

        return holder;

    }

    public String getDescription(JSONObject json){
        String holder;

        try{
            holder = json.getJSONObject("data").getString("description");

        } catch (JSONException e) {
            holder = "N/A";
        }

        return holder;

    }




    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                inputStream.close();
            } else {
                Log.d("JSON", "Failed to download file");
            }
        } catch (Exception e) {
            Log.d("readJSONFeed", e.getLocalizedMessage());
        }
        return stringBuilder.toString();
    }

}
/**
*由Mike于2015年2月28日创建。
*/
导入android.app.Activity;
导入android.app.Dialog;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.os.AsyncTask;
导入android.preference.PreferenceManager;
导入android.support.v7.app.ActionBarActivity;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.RatingBar;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.StatusLine;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
/**
*由Mike于2014年4月26日创建。
*/
公共类GetBeerDataJSON2扩展异步任务{
上下文c;
字符串id;
私人对话;
公共GetBeerDataJSON2(上下文,字符串beerID)
{
c=上下文;
id=beerID;
Dialog=新进度对话框(c);
}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//TODO自动生成的方法存根
返回readJSONFeed(arg0[0]);
}
受保护的void onPreExecute(){
setMessage(“获取啤酒信息”);
对话框.setTitle(“加载”);
Dialog.show();
}
受保护的void onPostExecute(字符串结果){
//在这里解码json
试一试{
JSONObject o=新的JSONObject(结果);
//获取啤酒详情
//todo:让我们从新的JSON中获得一些东西
字符串名称=o.getString(“名称”);
String beerDescription=o.getString(“description”);
字符串abv=o.getString(“abv”);
字符串ibu=o.getString(“ibu”);
字符串image=o.getString(“图标”);
玻璃线=o.getString(“玻璃”);
字符串beerBreweryStyle=o.getString(“样式”);
String beerBreweryName=o.getString(“啤酒厂”);
字符串状态=o.getString(“状态”);
字符串评级=o.getString(“评级”);
字符串食物=o.getString(“食物”);
int-beerRate=0;
beerRate=o.getInt(“用户评级”);
//准备按钮
//Button ButtonWebry=(Button)((活动)c).findViewById(R.id.ButtonWebry);
//按钮样式=(按钮)((活动)c).findViewById(R.id.buttonStyle);
TextView tv_breweryName=(TextView)((活动)c).findViewById(R.id.beerBreweryName);
TextView tv_styleName=(TextView)((活动)c).findViewById(R.id.beerStyleName);
SharedReferences preferences=PreferenceManager.GetDefaultSharedReferences(c);
SharedReferences.Editor=首选项.edit();
putString(“styleName”,beerBreweryStyle);
编辑器.putString(“beerName”,名称);
编辑器.putString(“lastBeer”,名称);
putString(“breweryName”,beerBreweryName);
putString(“styleName”,beerBreweryStyle);
commit();
((ActionBarActivity)getActivity()).getSupportActionBar().setTitle(名称);
//创建文本视图
TextView样式title=(TextView)((活动)c).findViewById(R.id.beerTitle);
styleTitle.setText(名称);
TextView textABV=(TextView)((活动)c).findViewById(R.id.abv);
text abv.setText(abv);
TextView textIBU=(TextView)((活动)c).findViewById(R.id.IBU);
textib.setText(ibu);
TextView textGlass=(TextView)((活动)c).findViewById(R.id.glass);
textGlass.setText(玻璃);
TextView textDescription=(TextView)((活动)c).findViewById(R.id.beerDescription);
textDescription.setText(beerDescription);
字符串breweryButton=“Brewery:”;
String styleButton=“Style:”;
//todo:将啤酒名称添加到共享首选项
tv_breweryName.setText(beerBreweryName);
tv_styleName.setText(beerBreweryStyle);
//获取用户id
SharedPreferences=PreferenceManager.getDefaultSharedPreferences(c);
字符串userName=prefs.getString(“userName”,null);
最终字符串userID=prefs.getString(“userID”,null);
//检查用户是否有啤酒
字符串url=”http://www.beerportfolio.com/app_checkBeer.php?";
字符串userURLComp=“u=”+userID;
最后一个字符串beerID=“&b=“+id;
url=url+userURLComp+beerID;
//新的CheckBeerJSON(c,id,userID).execute(url);
//todo:在此处添加检查啤酒的代码
如果(状态等于(“否”)){
//清除加载程序映像
LinearLayout ll=(LinearLayout)(LinearLayout)((活动)c).findViewById(R.id.addBeerLayout);
ll.removeAllViews();
//添加啤酒添加按钮
LayoutInflater mInflater=(LayoutInflater)c.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
LinearLayout addButton=(LinearLayout)((活动)c).findViewById(R.id.addBeerLayout);
addButton.addView(mInflater.充气(R.layout.addbeerbutton_layout,null));
//单击此处添加侦听器
按钮bt4=(按钮)(活动)c.findViewById(R.id.button1);
bt4.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共空间点击
public static ActionBarActivity instance = null;
onCreate(Bundle bundle){
GetBeerDataJSON2 task = new GetBeerDataJSON2() ; 
task.instance = this;
task.excute();
}
((ActionBarActivity)context).getSupportActionBar().setTitle(name);
((ActionBarActivity) context).getSupportActionBar().setTitle(name);
((ActionBarActivity)getActivity()).getSupportActionBar().setTitle(name);
public class MyAsyncTask extends AsyncTask<String, Void, String> {
  public interface TaskListener {
    void onTitleReceived(String title);
  }

  private final WeakReference<TaskListener> mTaskListenerRef;    

  public MyAsyncTask(TaskListener listener) {
    this.mTaskListenerRef = new WeakReference<>(listener);
  }

  public void notifyTitleReceived(String title) {
    TaskListener listener = mTaskListenerRef.get();
    if (listener != null) {
      listener.onTitleReceived(title);
    }
  }

  protected void onPostExecute(String result){
    ...
    notifyTitleReceived(newTitle);
    ...
  }
}

public class MyActivity extends ActionBarActivity implements MyAsyncTask.TaskListener {
    @Override
    public void onTitleReceived(String title) {
      getSupportActionBar().setTitle(title);
    }

    private void runTask() {
      new MyTask(this).execute();
    }
}