如何使用Layout来显示XML的结果

时间:2012-05-04 08:52:46

标签: android listview

我是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 /基准

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 /布局/ 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>

RES /布局/ 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>

2 个答案:

答案 0 :(得分:0)

只需使用数组适配器制作自定义列表视图。

参考这个例子..

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

答案 1 :(得分:0)

Web上充满了使用自定义适配器的ListView教程和示例。你将不得不花一些时间来学习它们。没有捷径。您可以使用其中一个here,也可以从亚马逊中选择其中一本书。