我在为联赛表创建布局时遇到了一些问题。在我当前的布局中,我创建了一个包含两行的表。第一行有我想要的标题,第二行有黑色字段,由获得的数据组成。我从我的服务器获取包含xml文件的数据。我遇到的问题是没有得到数据,因为我可以这样做..问题是,当我显示数据时,我使用ListAdapter,需要一个链接到它的布局。但是当我链接相应的布局时,我会在每一行之后重复标题...例如
TEAM P W L D P
阿森纳1 1 1 1 1
TEAM P W L D P
Aston Villa 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);
这是正确的????
我得到的第一个错误是(有一个很长的列表,但我认为这是原因):
04-15 09:46:42.268:E / AndroidRuntime(346):java.lang.RuntimeException:无法启动活动ComponentInfo {com.julian.football_app / com.julian.football_app.tab_1}:java.lang。 RuntimeException:您的内容必须具有一个ListView,其id属性为'android.R.id.list'
答案 0 :(得分: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>