我正在使用看起来像这样的Android应用程序:
(ListView开始)
名称 - 游戏
日期
链接(按钮)
Name2 - Game2
日期2
Link2(按钮)
(ListView结束)
在ListView中。 因此ListView Elemnt是:名称,游戏,日期(TextView)和链接(按钮) 所有数据都来自XML文件
所以我的问题: 如何获取按钮中的链接?像从XML文件中的link1到ListView Element1中的按钮等等。 已经检查了一些教程,但无法使用我的代码:
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>
和listplaceholder布局
<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>
答案 0 :(得分:0)
我刚刚添加了一个新的自适应适配器,它可以工作,所以如果有人正在寻找它:
public class DataAdapter extends ArrayAdapter<Data>
{
private ArrayList<Daten> items;
String url;
public DatenAdapter(Context context, int textViewResourceId, ArrayList<Daten> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_element, null);
}
Daten o = items.get(position);
url = o.getUrl();
if (o != null) {
TextView name = (TextView) v.findViewById(R.id.item_title);
TextView datum = (TextView) v.findViewById(R.id.item_date);
if (name != null) {
name.setText(o.getName()+" - "+o.getSpiel()); }
if(datum != null){
datum.setText("date: "+ o.getDate());
}
}
Button but = (Button) v.findViewById(R.id.buttonlink);
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
openWebURL(getUrl(position));
}
});
return v;
}
private String getUrl(int index) {
return d.get(index).getUrl();
}