片段

时间:2016-08-05 14:00:15

标签: android android-studio

我如何添加一个onback按下的按钮。在此活动上显示onBack按下按钮的图像但不起作用。我尝试了一些方法,但我无法解决我的问题。希望您能够帮助我。谢谢

这是代码:

    public class MyAdsActivity extends Fragment{


    public static MyAdsActivity newInstance(String param1, String param2) {
        MyAdsActivity fragment = new MyAdsActivity();
        return fragment;
    }

    public MyAdsActivity() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private OnFragmentInteractionListener mListener;

    Toolbar toolbar;
    ListView lsv;
    List<CatAdd> catAddList;
    CateAdDisplayAdapter adapter;
    Typeface typeface;
    String advertId;
    public static boolean refreshFlag = false;
    private SharedPreferences pref;

    private AdView mAdView;
    FloatingActionButton fabAddAds;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_myads, container, false);
        typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/GandhiSerif-Bold.otf");

        fabAddAds = (FloatingActionButton) view.findViewById(R.id.fabAddAds);

        mAdView = (AdView)view.findViewById(R.id.adView);
        mAdView.loadAd(new AdRequest.Builder().build());

        pref = this.getActivity().getSharedPreferences("loginpreference", getActivity().MODE_PRIVATE);
        advertId = pref.getString("advertId","");

        lsv = (ListView)view.findViewById(R.id.listView1);
        catAddList = new ArrayList<CatAdd>();
        fabAddAds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity() ,AddPostActivity.class));
            }
        });

        new MyAdTask().execute();

        lsv.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Intent intent = new Intent(getActivity(), BrowseMyDetailActivity.class);
                intent.putExtra("adId", String.valueOf(catAddList.get(arg2).getAddid()));
                startActivity(intent);
            }
        });

        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (refreshFlag) {
            refreshFlag = false;
            new MyAdTask().execute();
        }
    }

    class MyAdTask extends AsyncTask<Void, Void, Void>
    {
        String jsonStr = null;
        CustomProgressDialog cd = new CustomProgressDialog();
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            cd.showdialog(getActivity(), "Loading.....");
        }
        @Override
        protected Void doInBackground(Void... arg0) {
            ServiceHandler sh = new ServiceHandler();
            jsonStr = sh.makeServiceCall(String.format(Constants.MYADS_URL, advertId), ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    JSONArray contacts = jsonObj.getJSONArray(Constants.TAG);
                    catAddList.clear();
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);
                        String adId = c.getString(Constants.CAT_ADID);
                        String adTitle = c.getString(Constants.CAT_ADTITLE);
                        String adDes = c.getString(Constants.CAT_ADDES);
                        String adcity= c.getString(Constants.CAT_CITY);
                        String adPrise= c.getString(Constants.CAT_PRICE);

                        JSONArray arrImages=c.getJSONArray("images");
                        ArrayList<String> imgArray=new ArrayList<String>();
                        for(int j=0;j<arrImages.length();j++)
                        {
                            JSONObject imgObj=arrImages.getJSONObject(j);
                            if(imgObj.has("imageName"))
                            {
                                imgArray.add(imgObj.getString("imageName"));
                            }

                        }
                        CatAdd v=new CatAdd();
                        v.setAddid(Integer.parseInt(adId));
                        v.setAdTitle(adTitle);
                        v.setAdDesc(adDes);
                        v.setAdPrice(adPrise);
                        v.setImglist(imgArray);
                        v.setAdCity(adcity);
                        catAddList.add(v);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            adapter = new CateAdDisplayAdapter(getActivity(), catAddList);
            adapter.notifyDataSetChanged();
            lsv.setAdapter(adapter);
            cd.dismissdialog();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我无法理解你...你有一个Button并希望当你点击它关闭活动或什么?如果是这样,那么写下这段代码:

Button yourButton = (Button) view.findViewById(R.id.your_button_id);
yourButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });