2012-04-13 36 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>