在导航栏中默认显示主片段

时间:2015-04-10 15:54:33

标签: android

在我的Android应用程序中,我使用导航抽屉。我想显示我的第一个片段(Home)作为默认值。我让它工作。但我还想显示这个片段的方法和功能。

这是我的主页片段:

public class Home extends Fragment {
ImageView hotelpic;
TextView hotelname,hoteldescription;
private static final String TAG_DESCRIPTION_HOTEL = "category";
private static final String TAG_HOTEL_NAME = "title";
private static final String TAG_HOTEL_lINK = "link";
private static final String TAG_IMAGE_HOTEL = "image";
JSONArray hotel = null;
private String url = "http://myserver/api/etablissements/etablissement_hotel/id/";
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES="My profile info";
public static final String keyidhotel="idH";private String myidhotel;
public Home(){}

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

    View view = inflater.inflate(R.layout.home, container, false);

    sharedpreferences =  getActivity().getApplicationContext().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    if (sharedpreferences.contains(keyidhotel)){
        myidhotel = sharedpreferences.getString(keyidhotel,"");
        /*TextView myid=(TextView)view.findViewById(R.id.idhotel);
        myid.setText(myidhotel);*/

    }
    url=url+myidhotel;
    Toast.makeText(getActivity().getApplicationContext(), "URL HOME : " + url, Toast.LENGTH_LONG).show();

    hotelname=(TextView)view.findViewById(R.id.hotelname);
    hoteldescription=(TextView)view.findViewById(R.id.category);
    hotelpic = (ImageView)view.findViewById(R.id.imghome);

    new JSONParse().execute();

    return view;
}


class JSONParse extends AsyncTask<String, String, JSONObject> {
    ProgressDialog dialog;


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("Loading, please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        dialog.cancel();

        try {


            hotel = json.getJSONArray("hotel");
            JSONObject c = hotel.getJSONObject(0);
            String title = c.getString(TAG_HOTEL_NAME);
            String description = c.getString(TAG_DESCRIPTION_HOTEL);
            String link = c.getString(TAG_HOTEL_lINK);
            String root="/images/nearby/thumbs/";
            String imagename = c.getString(TAG_IMAGE_HOTEL);
            if(imagename.equals("null")||(imagename.equals(""))){
                hotelpic.setImageResource(R.drawable.nophoto); //sans effet
            }
            else {
                String imageurl = link + root + imagename;
                new DownloadImageTask(hotelpic).execute(imageurl);
            }

            hotelname.setText(title);
         hoteldescription.setText(description);


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {

            bmImage.setImageBitmap(result);
        }

    }


    @Override
    protected JSONObject doInBackground(String... params) {
        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
}

}

0 个答案:

没有答案