Java tutorial
/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package br.com.hype.medan; import android.app.ProgressDialog; import android.net.ParseException; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.widget.Toast; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.util.ArrayList; /** * Fragment that displays "Tuesday". */ public class ThisFreeFragment extends Fragment { ArrayList<Event> eventList; EventAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment //This layout contains your list view View view = inflater.inflate(R.layout.fragment_happening, container, false); eventList = new ArrayList<Event>(); new JSONAsyncTask().execute("http://api.hypemedan.id/androidapi/list_event_happening.php"); //now you must initialize your list view ListView listview = (ListView) view.findViewById(R.id.lv_item); adapter = new EventAdapter(getActivity(), R.layout.lsv_item_event, eventList); listview.setAdapter(adapter); //To have custom list view use this : you must define CustomeAdapter class // listview.setadapter(new CustomeAdapter(getActivity())); //getActivty is used instead of Context return view; } class JSONAsyncTask extends AsyncTask<String, Void, Boolean> { 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 Boolean doInBackground(String... urls) { try { //------------------>> HttpGet httppost = new HttpGet(urls[0]); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httppost); // StatusLine stat = response.getStatusLine(); int status = response.getStatusLine().getStatusCode(); if (status == 200) { HttpEntity entity = response.getEntity(); String data = EntityUtils.toString(entity); JSONObject jsono = new JSONObject(data); JSONArray jarray = jsono.getJSONArray("event"); for (int i = 0; i < jarray.length(); i++) { JSONObject object = jarray.getJSONObject(i); Event event = new Event(); event.setId(object.getString("pid")); event.setNama(object.getString("nama")); event.setGambar(object.getString("gambar")); eventList.add(event); } return true; } //------------------>> } catch (ParseException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return false; } protected void onPostExecute(Boolean result) { dialog.cancel(); adapter.notifyDataSetChanged(); if (result == false) Toast.makeText(getActivity(), "Unable to fetch data from server", Toast.LENGTH_LONG).show(); } } }