【发布时间】:2012-05-07 12:18:34
【问题描述】:
我正在开发如下所示的 android 应用:
(列表视图开始)
名称 - 游戏
日期
链接(按钮)
名称2 - 游戏2
日期2
Link2(按钮)
(列表视图结束)
在 ListView 中。 所以 ListView 元素是:名称、游戏、日期(TextView)和链接(按钮) 所有数据都来自一个 XML 文件
所以我的问题: 如何获取按钮中的链接?比如从 XML 文件到 ListView Element1 中的按钮的链接 1 等等。 已经检查了一些教程,但无法使用我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getXML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(Main.this, "...", Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("result");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
if(i==0){
TextView update= (TextView) findViewById(R.id.textView6);
update.setText("last Update: "+XMLfunctions.getValue(e, "update"));
} else {
map.put("name", XMLfunctions.getValue(e, "name")+": " + XMLfunctions.getValue(e, "game") );
map.put("date", XMLfunctions.getValue(e, "date"));
mylist.add(map);
}
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "date" },
new int[] { R.id.item_title, R.id.item_date });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
编辑
我解析的xml示例:
<result>
<id>1</id>
<name>test</name>
<game>gamename</game>
<date>27.04</date>
<link>www.google.com</link>
</result>
<result>
<id>2</id>
<name>test2</name>
<game>gamename2</game>
<date>27.04</date>
<link>www.google.com</link>
</result>
和列表占位符布局
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="280dp"
android:drawSelectorOnTop="false" android:scrollbars="horizontal|vertical"/>
和主布局(ListView的布局)
<TableLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/item_title"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:padding="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="10dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/item_date"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:padding="2dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="10dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/buttonlink"
style="?android:attr/buttonStyleSmall"
android:layout_width="10dp" android:layout_height="fill_parent"
android:text="Link"
/>
</TableRow>
</TableLayout>
【问题讨论】:
-
请发布您读取数据的XML文件的格式和您的main.xml。
标签: android xml listview button