2012-04-13 10 views
0

Ich habe einige Frage ein Layout für eine Tabelle zu erstellen. In meinem aktuellen Layout habe ich eine Tabelle mit zwei Zeilen erstellt. Die erste Zeile enthält die gewünschten Überschriften und die zweite Zeile enthält schwarze Felder, die aus den erhaltenen Daten bestehen. Ich bekomme die Daten von meinem Server, der eine XML-Datei enthält. Das Problem, das ich habe, ist, immer die Daten nicht, wie ich das tun kann .. Das Problem ist, dass, wenn ich die Daten anzeigen ich einen ListAdapter verwenden, die ein Layout mit ihm verbunden benötigen. Aber wenn ich das passende Layout verlinke, bekomme ich eine Wiederholung des Headers nach jeder Zeile ... z.Layout für eine Tabelle

TEAM PWLDP

Arsenal 1 1 1 1 1

TEAM PWLDP

Aston Villa 1 1 1 1 1

Ich bin sicher, es ist ein einfacher Fehler zu beheben ... vielleicht muss ich dir etwas anderes zum anzeigen der daten ???

Unten finden Sie den Code für die Haupt

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

Im Folgenden wird das Layout 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> 

Im Folgenden wird die loyout für ListPlaceHolder.XML

genannt
<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 .. also habe ich das Layout der main_tab.xml geändert und ein neues Layout namens table_header.xml erstellt, das die Überschriften für die Liga enthält, z. Name, Gespielt, Won, Verloren etc ... Aber ich habe jetzt ein Fehler in der logcat .. Unten ist der Code, was für Haupt-Seite ...

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

       } 
      }); 

Die Linie zum Ändern des Layouts i verwendet hat, ist : setContentView (R.layout.table_header);

ist das richtig ????

Der erste Fehler, den ich bekommen ist (es gibt eine lange Liste, aber ich denke, dies ist die Ursache ist):

September 4-15: 46: 42,268: E/Android Runtime (346): java.lang. Runtime: Kann Aktivität ComponentInfo starten {com.julian.football_app/com.julian.football_app.tab_1}: java.lang.RuntimeException: Ihr Inhalt ein Listview, deren id-Attribut ist ‚android.R.id.list‘

haben muss
+0

Wenn Sie genau hinschauen sehen Sie einige fehlende} am Ende des ersten Code -Klammern. Ich selbst würde auch mehr Leerzeichen verwenden, um das Xml-Teil –

+0

Problem zu löschen ist, dass es kein Tabellenlayout mit Adaptern – njzk2

+0

@ julian9876 funktioniert, da Sie neu sind Ich dachte, ich würde erwähnen, dass es gute Manieren hier ist, um eine Antwort zu markieren akzeptiert, wenn es Ihr Problem löst. Der Grund ist zweifach. Erstens lässt es jeden wissen, dass dein Problem gelöst ist, und zweitens gibt es der Person, die dir geholfen hat, Anerkennung. – Barak

Antwort

0

Wie Sie vermutet, Ihr Problem ist hier:

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

Insbesondere folgt aus:

R.layout.main_tab 

, die angeblich die Liste Reihe Layout sein. Da Sie dies mit Header und Daten angegeben haben, wird der Header für jede Listenzeile wiederholt. Trennen Sie. Sie müssen ein Layout für JUST die Liste erstellen. Entfernen Sie im Prinzip das zweite TableLayout aus dieser Datei, legen Sie es in eine eigene XML-Datei und rufen Sie dieses für Ihren Listenadapter auf. Fügen Sie Ihr ListView unterhalb des Header-Tabellenlayouts in main_tab hinzu.

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>