2015-11-28 181 views
1

我想創建一個應用程序,顯示在我的學校每個餐廳的菜單。我將如何去解析這個網站https://hdh.ucsd.edu/DiningMenus/default.aspx?i=18https://hdh.ucsd.edu/DiningMenus/default.aspx?i=18並將早餐午餐和晚餐的菜單變成顯示在片段上的列表視圖? 這是我的代碼有: ErcFragment.java如何解析一個網站,並顯示在列表視圖片段android

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> 
+0

你必須使用JSON或XML解析從服務器獲取數據,並可以使用你的數據在你的應用 – sud

+0

遵循這個答案的解決方案[http://stackoverflow.com/questions/16545378/how-can-我取入數據從-A-web服務器功能於一個-機器人的應用](http://stackoverflow.com/questions/16545378/how-can-i-fetch-data-from-a-web -server-in-an-android-application) – sud

+0

@sud他在這裏沒有處理api,他解析了HTML。 –

回答

0

如果你successfuly正從服務器的數據按照下面的代碼清單片段 - 使其顯示

以下是示例代碼。

首先爲一個列表行創建一個佈局。

例如: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); 
相關問題