我在为排行榜创建布局时遇到了一些问题。在我当前的布局中,我创建了一个包含两行的表。第一行有我想要的标题,第二行有黑色字段,其中包含获得的数据。我从包含 xml 文件的服务器获取数据。我遇到的问题是没有像我那样获取数据。问题是当我显示数据时,我使用了一个需要链接到它的布局的 ListAdapter。但是当我链接适当的布局时,我会在每一行之后重复标题...例如
团队 PWLDP
阿森纳 1 1 1 1 1
团队 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 的配置
<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: 你的content 必须有一个 id 属性为 'android.R.id.list' 的 ListView