我是 Android 的新人。我尝试开发一个列表,可以在同一行显示歌曲名称和作者。源数据来自在线 XML 文件
以下是我尝试在我的程序中使用的代码。但是,我只能在列表中显示作者姓名。我想问我应该如何修改代码,以便我可以在列表的同一行显示歌曲名称和作者?
我尝试阅读的在线 XML 文件的格式
<recipes>
<song>
<id>1</id>
<title>Sing A Song</title>
<songs_author>ACM</songs_author>
</song>
<song>
<id>2</id>
<title>DO Re Me</title>
<songs_author>BBC/songs_author>
</song>
</recepies>
src/com.mobile/SongsActivity
package com.mobile;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class SongsActivity extends Activity implements OnItemClickListener {
protected static final int DIALOG_KEY = 0;
ListView mListView;
Button mClear;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
this.setProgressBarIndeterminateVisibility(false);
setContentView(R.layout.Songs);
mListView = (ListView) findViewById(R.id.listView1);
mListView.setTextFilterEnabled(true);
mListView.setOnItemClickListener(this);
LoadRecipesTask1 mLoadRecipesTask = new LoadRecipesTask1();
mLoadRecipesTask.execute("http://123.com/mobile/Songs_list.php");
mClear = (Button) findViewById(R.id.button3);
mClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListView.setAdapter(null);
}
});
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Datum datum = (Datum) mListView.getItemAtPosition(position);
Uri uri = Uri.parse("http://123.com/mobile/Songs.php?Songsid=" + datum.getId());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
this.startActivity(intent);
}
public static ArrayList<Datum> parse(String url) throws IOException, XmlPullParserException {
final ArrayList<Datum> results = new ArrayList<Datum>();
URL input = new URL(url);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(input.openStream(), null);
int eventType = xpp.getEventType();
String currentTag = null;
Integer id = null;
String title = null;
String Songs_author = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
currentTag = xpp.getName();
} else if (eventType == XmlPullParser.TEXT) {
if ("id".equals(currentTag)) {
id = Integer.valueOf(xpp.getText());
}
if ("title".equals(currentTag)) {
title = xpp.getText();
}
if ("Songs_author".equals(currentTag)) {
Songs_author = xpp.getText();
}
} else if (eventType == XmlPullParser.END_TAG) {
if ("song".equals(xpp.getName())) {
results.add(new Datum(id, title, Songs_author));
}
}
eventType = xpp.next();
}
return results;
}
protected class LoadRecipesTask1 extends AsyncTask<String, Void, ArrayList<Datum>> {
@Override
protected void onPreExecute() {
SongsActivity.this.setProgressBarIndeterminateVisibility(true);
}
@Override
protected ArrayList<Datum> doInBackground(String... urls) {
ArrayList<Datum> datumList = new ArrayList<Datum>();
try {
datumList = parse(urls[0]);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return datumList;
}
@Override
protected void onPostExecute(ArrayList<Datum> result) {
mListView.setAdapter(new ArrayAdapter<Datum>(SongsActivity.this, R.layout.list_item, result));
//SongsActivity.this.setProgressBarIndeterminateVisibility(false);
}
}
}
src/com.mobile/Datum
package com.mobile;
public class Datum {
int id;
String title;
String songs_author;
public Datum(int id, String title, String songs_author) {
this.id = id;
this.title = title;
this.songs_author= songs_author;
}
public String toString() {
return songs_author;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String get Songs_author() {
return Songs_author;
}
}
res/layout/songs.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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="Refresh 1" android:id="@+id/button1"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:text="Clear" android:id="@+id/button3"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"></Button>
</LinearLayout>
<ListView android:id="@+id/listView1" android:layout_height="fill_parent"
android:layout_width="fill_parent"></ListView>
</LinearLayout>
资源/布局/list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>