0

I'm having some issue creating a layout for a league table. In my current layout, i have created a table with consists of two rows. The first row has the headings i want, and the second row has black fields that consist of the data that is obtained. I get the data from my server which contains an xml file. The problem i am having is not getting the data as i can do that.. The problem is that when i display the data i use a ListAdapter which need a layout linked to it. But when i link the appropriate layout, i get a repeat of header after every line... e.g.

TEAM P W L D P

Arsenal 1 1 1 1 1

TEAM P W L D P

Aston Villa 1 1 1 1 1

I'm sure its a simple error to fix.... maybe i have to you something else to display the data???

Below is the code for the main

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();
                }
            });
        }

Below is the layout called 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>

Below is the loyout for ListPlaceHolder.XML

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

Ok cool.. so i have changed the layout of the main_tab.xml and created a new layout called table_header.xml That contains the headings for the league e.g. Name, Played, Won, Lost etc... But now i have an error in the logcat.. Below is the code what for main page...

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(); 

                }
            });

The line for changing the layout i have used is: setContentView(R.layout.table_header);

is this correct????

The first error i get is (there is a long list, but i think this is the cause):

04-15 09:46:42.268: E/AndroidRuntime(346): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.julian.football_app/com.julian.football_app.tab_1}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
julian9876
  • 61
  • 1
  • 8
  • If you look carefully you see some missing }-brackets at the end of your first code. I myself would also use more spaces to clear the xml-part – Shegit Brahm Apr 13 '12 at 14:18
  • problem is that there is no table layout working with adapters – njzk2 Apr 13 '12 at 16:00
  • @julian9876, since you are new I thought I'd mention that it's good manners around here to mark an answer as accepted if it solves your problem. Reason is two-fold. First, it lets everyone know your problem is solved and second it gives credit to the person that helped you. – Barak Apr 13 '12 at 21:01
  • @njzk2, you can use any kind of layout with a list, it just depends on how much work you want to do. – Barak Apr 13 '12 at 21:04

1 Answers1

0

As you suspected, your problem is here:

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});

Specifically this:

R.layout.main_tab

That is supposed to be the list row layout. Since you provided that with header and data, it is repeating the header for each list line. Separate them. You need to create a layout for JUST the list. Basically remove the second TableLayout from that file, put it in it's own XML file and call that one for your list adapter. Add your ListView underneath the header table layout in main_tab.

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>
Barak
  • 16,318
  • 9
  • 52
  • 84