2012-04-13 34 views
0

我在創建聯盟表的佈局時遇到了一些問題。在我目前的佈局中,我創建了一個由兩行組成的表格。第一行具有我想要的標題,第二行具有由獲得的數據組成的黑色字段。我從包含xml文件的服務器獲取數據。我遇到的問題是沒有獲取數據,因爲我可以做到這一點..問題是,當我顯示數據我使用ListAdapter需要一個佈局鏈接到它。但是當我鏈接適當的佈局時,我會在每行之後重複標題...例如聯盟表的佈局

TEAM PWLDP

阿森納1 1 1 1 1

TEAM PWLDP

阿斯頓維拉1 1 1 1 1

我敢肯定它的一個簡單的錯誤修復... 。也許我不得不給你別的東西來顯示數據?

下面是主要的

public class tab_1 extends ListActivity { 

    public int a = Football_appActivity.league; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listplaceholder); 

     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 

     if(a==1){ 
      String xml = XMLfunctions.getXML_prem_table(); 
      Document doc = XMLfunctions.XMLfromString(xml); 
      int numResults = XMLfunctions.numResults(doc); 

      if((numResults <= 0)){ 
       Toast.makeText(tab_1.this, "League Table", Toast.LENGTH_LONG).show(); 
       finish(); 
      } 

      NodeList nodes = doc.getElementsByTagName("team"); 

      for (int i = 0; i < nodes.getLength(); i++) { 
       HashMap<String, String> map = new HashMap<String, String>(); 

       Element e = (Element)nodes.item(i); 
       map.put("id", XMLfunctions.getValue(e, "id")); 
       map.put("name", "" + XMLfunctions.getValue(e, "name")); 
       map.put("played", "" + XMLfunctions.getValue(e, "played")); 
       map.put("won", "" + XMLfunctions.getValue(e, "won")); 
       map.put("drawn", "" + XMLfunctions.getValue(e, "drawn")); 
       map.put("lost", "" + XMLfunctions.getValue(e, "lost")); 
       map.put("points", "" + XMLfunctions.getValue(e, "points")); 
       mylist.add(map); 
      } 
      ** 
      ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main_tab, 
       new String[] { "name", "played", "won", "drawn", "lost", "points"}, 
       new int[] { R.id.item_title, R.id.item_played, R.id.item_won, 
       R.id.item_drawn, R.id.item_lost, R.id.item_points});** 

      setListAdapter(adapter); 

      final ListView lv = getListView(); 
      lv.setTextFilterEnabled(true); 
      lv.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        @SuppressWarnings("unchecked") 
        HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position); 
        Toast.makeText(tab_1.this, o.get("name") + " have Won '" + o.get("won") + "' games, and are currently on '" + o.get("points") + "' points!", Toast.LENGTH_LONG).show(); 
       } 
      }); 
     } 

下面的代碼是所謂main_tab.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="7dp" 
> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:stretchColumns="1" 
android:id="@+id/table123"> 
<TableRow> 
<TextView 
    android:text="Name" 
    android:id="@+id/team_title"> 
    </TextView> 
<TextView android:text="P " 
    android:gravity="right" 
    android:id="@+id/team_played"> 
</TextView> 
<TextView android:text="W" 
    android:id="@+id/team_won"> 
</TextView> 
<TextView android:text="D" 
    android:id="@+id/team_drawn"> 
</TextView> 
<TextView android:text="L" 
    android:id="@+id/team_lost"> 
</TextView> 
<TextView android:text="P" 
    android:id="@+id/team_points"> 
</TextView> 
</TableRow> 

<TableRow> 
    <TextView 
     android:id="@+id/item_title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:padding="2dp" 
     android:textSize="20dp" /> 
     <TextView 
     android:id="@+id/item_played" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="right" 
     android:padding="2dp" 
     android:textSize="13dp" /> 
     <TextView 
     android:id="@+id/item_won" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="2dp" 
     android:textSize="13dp" /> 
     <TextView 
     android:id="@+id/item_drawn" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="2dp" 
     android:textSize="13dp" /> 
     <TextView 
     android:id="@+id/item_lost" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="2dp" 
     android:textSize="13dp" /> 
     <TextView 
     android:id="@+id/item_points" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="2dp" 
     android:textSize="13dp" /> 
     </TableRow> 
     </TableLayout> 

</LinearLayout> 

下面的佈局是爲ListPlaceHolder.XML

的loyout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">  

<ListView 
    android:id="@id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:drawSelectorOnTop="false" /> 

<TextView 
    android:id="@id/android:empty" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="No data"> 
    </TextView> 

</LinearLayout> 

好酷..所以我改變了main_tab.xml的佈局,並創建了一個名爲table_header.xml的新佈局,它包含了聯盟的標題,例如,名稱,玩過勝負等...但現在我有在logcat的錯誤..下面是什麼主頁的代碼...

if(a==1){ 
      String xml = XMLfunctions.getXML_prem_table(); 
      Document doc = XMLfunctions.XMLfromString(xml); 
      int numResults = XMLfunctions.numResults(doc); 

      if((numResults <= 0)){ 
       Toast.makeText(tab_1.this, "League Table", Toast.LENGTH_LONG).show(); 
       finish(); 
      } 

      NodeList nodes = doc.getElementsByTagName("team"); 

      setContentView(R.layout.table_header); 

      for (int i = 0; i < nodes.getLength(); i++) {       
       HashMap<String, String> map = new HashMap<String, String>();  

       Element e = (Element)nodes.item(i); 
       map.put("id", XMLfunctions.getValue(e, "id")); 
       map.put("name", "" + XMLfunctions.getValue(e, "name")); 
       map.put("played", "" + XMLfunctions.getValue(e, "played")); 
       map.put("won", "" + XMLfunctions.getValue(e, "won")); 
       map.put("drawn", "" + XMLfunctions.getValue(e, "drawn")); 
       map.put("lost", "" + XMLfunctions.getValue(e, "lost")); 
       map.put("points", "" + XMLfunctions.getValue(e, "points")); 
       mylist.add(map);    
      }  

      ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main_tab, 
          new String[] { "name", "played", "won", "drawn", "lost", "points"}, 
          new int[] { R.id.item_title, R.id.item_played, R.id.item_won, 
             R.id.item_drawn, R.id.item_lost, R.id.item_points}); 

      setListAdapter(adapter); 

      final ListView lv = getListView(); 
      lv.setTextFilterEnabled(true); 
      lv.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
        @SuppressWarnings("unchecked") 
        HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);     
        Toast.makeText(tab_1.this, o.get("name") + " have Won '" + o.get("won") + "' games, and are currently on '" + o.get("points") + "' points!", Toast.LENGTH_LONG).show(); 

       } 
      }); 

我已經用於改變佈局的行是: setContentView(R.layout.table_header);

這是正確的嗎?

的第一個錯誤我得到的是(有一個長長的清單,但我認爲這是原因):

9月4日至15日:46:42.268:E/AndroidRuntime(346):java.lang中。 RuntimeException:無法啓動活動ComponentInfo {com.julian.football_app/com.julian.football_app.tab_1}:java.lang.RuntimeException:您的內容必須具有List屬性爲'android.R.id.list'的ListView

+0

如果你仔細看,你看到一些缺少} -brackets在你的第一個程序結束。我自己也會使用更多的空間來清除xml部分 – 2012-04-13 14:18:13

+0

問題是沒有表格佈局與適配器 – njzk2 2012-04-13 16:00:01

+0

@ julian9876一起工作,因爲你是新的,我想我會提到這是在這裏標記答案的良好態度接受,如果它解決了你的問題。原因是雙重的。首先,它讓每個人都知道你的問題已經得到解決,其次,它給予了幫助你的人的功勞。 – Barak 2012-04-13 21:01:52

回答

0

當你懷疑,你的問題就在這裏:

ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main_tab, 
    new String[] { "name", "played", "won", "drawn", "lost", "points"}, 
    new int[] { R.id.item_title, R.id.item_played, R.id.item_won, 
    R.id.item_drawn, R.id.item_lost, R.id.item_points}); 

具體是:

R.layout.main_tab 

這應該是列表佈局。由於您提供了標題和數據,因此它將重複每個列表行的標題。將它們分開。你只需要爲列表創建一個佈局。基本上從該文件中刪除第二個TableLayout,把它放在它自己的XML文件中,併爲你的列表適配器調用它。在main_tab的頭部表格佈局下添加你的ListView。

row_layout.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myTableLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TableRow> 
    <TextView 
      android:id="@+id/item_title" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:padding="2dp" 
      android:textSize="20dp" /> 
    <TextView 
      android:id="@+id/item_played" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:padding="2dp" 
      android:textSize="13dp" /> 
    <TextView 
      android:id="@+id/item_won" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="2dp" 
      android:textSize="13dp" /> 
    <TextView 
      android:id="@+id/item_drawn" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="2dp" 
      android:textSize="13dp" /> 
    <TextView 
      android:id="@+id/item_lost" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="2dp" 
      android:textSize="13dp" /> 
    <TextView 
      android:id="@+id/item_points" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="2dp" 
      android:textSize="13dp" /> 
      </TableRow> 
    </TableLayout>