1

I am trying to create an app that displays the menus of each dining hall at my school. How would I go about parsing this website https://hdh.ucsd.edu/DiningMenus/default.aspx?i=18 or https://hdh.ucsd.edu/DiningMenus/default.aspx?i=18 and getting the menus for breakfast lunch and dinner into a listview that displays on a fragment? This is the code I have: ErcFragment.java

import android.view.View;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ListView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.os.AsyncTask;
import android.app.ProgressDialog;
import android.widget.TextView;
import java.util.ArrayList;


public class ErcFragment extends Fragment {

// URL Address
String url = "http://hdh.ucsd.edu/mobile/dining/locationdetails.aspx?l=18&no_server_init";
ProgressDialog mProgressDialog;
String MENU;
ListView listview;
TextView textview;

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

    View rootView = inflater.inflate(R.layout.fragment_erc, container, false);

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

    textview = (TextView)view.findViewById(R.id.textView10);

    new JSOUP().execute();
    return rootView;

}

public class JSOUP extends AsyncTask<Void, Void, Void>{
    ProgressDialog dialog;

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("loading...");
        dialog.show();

    }

    @Override
    protected Void doInBackground(Void... params){
        try{

            Document document = Jsoup.connect(url).get();
            Elements elements = document.select("div[class='message info last']");
            for(int i = 0; i<elements.size(); i++){
                MENU += "\n" + elements.get(i).text();
            }
        }

        catch (Exception e){

        }
        return null;
    }



    @Override
    protected void onPostExecute(Void result){
        dialog.dismiss();
        textview.setText(MENU);
        super.onPostExecute(result);
    }



}

}

and fragment_erc.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"   android:layout_height="match_parent">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Cafe Ventanas"
    android:id="@+id/textView9"
    android:layout_gravity="center_horizontal"
    android:elegantTextHeight="false"
    android:textSize="40dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView10"
    android:layout_gravity="center" />


</FrameLayout>
Lam Pham
  • 39
  • 1
  • 1
  • 4
  • you have to use json or xml parsing to get data from server and can use your data in your app – sud Nov 28 '15 at 05:01
  • follow this answer for solution [http://stackoverflow.com/questions/16545378/how-can-i-fetch-data-from-a-web-server-in-an-android-application](http://stackoverflow.com/questions/16545378/how-can-i-fetch-data-from-a-web-server-in-an-android-application) – sud Nov 28 '15 at 05:03
  • @sud He's not dealing with an api here, he's parsing the HTML. – Rami Jemli Nov 28 '15 at 05:13
  • @Lam Pham With this code, are you able to get the document ? – Rami Jemli Nov 28 '15 at 05:15
  • @RamiJemli How would I check my document? – Lam Pham Nov 28 '15 at 05:33

1 Answers1

0

if you are successfuly getting data from server the follow the below code to display it in list fragment-

Here is the example code.

First create a layout for one list row.

example : list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    >
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/merk"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_marginTop="5dp" />

    <TextView
        android:id="@+id/harga"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/merk"
        android:layout_marginTop="5dp"/>

</RelativeLayout>

After creating this list_row.xml layout, create a adapter class.

create CustomListAdapter.java

public class CustomListAdapter extends BaseAdapter {

private Activity activity;
private LayoutInflater inflater;
private ArrayList<Barang> barangList;

public CustomListAdapter(Activity activity, ArrayList<Barang> barangList) {
    this.activity = activity;
    this.barangList = barangList;
}

/*
get count of the barangList
 */

@Override
public int getCount() {
    return barangList.size();
}

@Override
public Object getItem(int location) {
    return barangList.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

/*
inflate the items in the list view
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null) {
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_row, null);
    }


    /*
    creating objects to access the views
     */
    TextView name = (TextView) convertView.findViewById(R.id.name);
    TextView merk = (TextView) convertView.findViewById(R.id.merk);
    TextView harga = (TextView) convertView.findViewById(R.id.harga);



    // getting barang data for the row

    Barang barang = barangList.get(position);

    name.setText(barang.getNama_barang());

    merk.setText(barang.getMerk_barang());

    harga.setText(barang.getHarga_barang());

    return convertView;
}}

Now in your MasterBarang.java, put the following code in your onCreate method.

values = dataSource.getAllBarang();
CustomListAdapter adapter;
adapter = new CustomListAdapter(getActivity(), values);
setListAdapter(adapter);
sud
  • 505
  • 1
  • 4
  • 12