如何解析网站并在listview片段中显示android

时间:2015-11-28 04:58:29

标签: android listview android-fragments fragment

我正在尝试创建一个应用程序,显示我学校每个餐厅的菜单。我将如何解析此网站https://hdh.ucsd.edu/DiningMenus/default.aspx?i=18https://hdh.ucsd.edu/DiningMenus/default.aspx?i=18并将早餐午餐和晚餐的菜单放入显示在片段上的列表视图中? 这是我的代码: 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);
    }



}

}

和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>

1 个答案:

答案 0 :(得分:0)

如果您成功从服务器获取数据,请按照以下代码将其显示在列表片段 -

以下是示例代码。

首先为一个列表行创建一个布局。

示例: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>

创建此list_row.xml布局后,创建一个适配器类。

创建CustomListAdapter.java

公共类CustomListAdapter扩展了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;
}}

现在在MasterBarang.java中,将以下代码放入 onCreate 方法中。

values = dataSource.getAllBarang();
CustomListAdapter adapter;
adapter = new CustomListAdapter(getActivity(), values);
setListAdapter(adapter);