1

嗨,我是 android 的新手,现在正在开发一个应用程序来列出来自 RSS FEED 解析的一些日期,而所有其他工作正常的问题是我想将图像添加到列表视图项中。内容取自 RSS 提要。但 Rss 提要不包含任何图像。我需要将图像添加到列表视图中。而且所有列表项的图像都是相同的。

解析是使用 DefaultHandler 完成的...

这是我的列表项页面

public class MainActivity extends Activity {


    // A reference to the local object
    private MainActivity local;

    /** 
     * This method creates main application view
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set view
        setContentView(R.layout.activity_main);


        // Set reference to this activity
        local = this;

        GetRSSDataTask task = new GetRSSDataTask();

        // Start download RSS task
        task.execute("http://manna.christianchannel.us/feed/");



        // Debug the thread name
        Log.d("Reader", Thread.currentThread().getName());
    }

    private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
        @Override
        protected List<RssItem> doInBackground(String... urls) {

            // Debug the task thread name
            Log.d("Reader", Thread.currentThread().getName());

            try {
                // Create RSS reader
                RssReader rssReader = new RssReader(urls[0]);

                // Parse RSS, get items
                return rssReader.getItems();

            } catch (Exception e) {
                Log.e("Reader", e.getMessage());
            }

            return null;
        }

        @Override
        protected void onPostExecute(List<RssItem> result) {

            // Get a ListView from main view
            ListView itcItems = (ListView) findViewById(R.id.listMainView);

            // Create a list adapter
            ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,android.R.layout.simple_list_item_1, result);
            // Set list adapter for the ListView
            itcItems.setAdapter(adapter);

            // Set list view item click listener
            itcItems.setOnItemClickListener(new ListListeners(result, local));
        }
    }

}

这是我的 RssParseHandler 页面

public class RssParseHandler extends DefaultHandler {

private List<RssItem> rssItems;

// Used to reference item while parsing
private RssItem currentItem;

// Parsing title indicator
private boolean parsingTitle;
// Parsing link indicator
private boolean parsingLink;
private boolean parsingDescription;

public RssParseHandler() {
    rssItems = new ArrayList<RssItem>();
}

public List<RssItem> getItems() {
    return rssItems;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    if ("item".equals(qName)) {
        currentItem = new RssItem();
    } else if ("title".equals(qName)) {
        parsingTitle = true;
    } else if ("link".equals(qName)) {
        parsingLink = true;
    }else if ("description".equals(qName)) {

            parsingDescription =true;


    }

}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {

    if ("item".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } else if ("title".equals(qName)) {
        parsingTitle = false;
    } else if ("link".equals(qName)) {
        parsingLink = false;
    } else if ("description".equals(qName)) {
        parsingDescription=false;

    }
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    if (parsingTitle) {
        if (currentItem != null)
            currentItem.setTitle(new String(ch, start, length));
    } else if (parsingLink) {
        if (currentItem != null) {
            currentItem.setLink(new String(ch, start, length));
            parsingLink = false;
        }
    } else if (parsingDescription) {
        if (currentItem !=null) {
            currentItem.setDescription(new String(ch, start, length));
            parsingDescription =false;
        }
    } 
}

}

这是我的 RSS 项目页面

public class RssItem {
// item title
    private String title;
    // item link
    private String link;
    private String description;
    private String icons;

    public String getIcons() {
        return icons;
    }

    public void setIcons(String icons) {
        this.icons = icons;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    @Override
    public String toString() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

4

1 回答 1

1

添加到@Michael 的评论。

您需要为您的项目创建自己的布局ListView,以便将相同的图像添加到所有行。

以下是 XML 的示例以及如何Activity创建:

活动主.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</RelativeLayout>

ListView 行项目(listview_item.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

和 MainActivity.java 文件:

package com.example.listviewtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView1);

        String[] arr = new String[] { "Android 2.3", "Android 2.3.3",
            "Android 3.1", "Android 4.0.4", "Android 4.1.2",
            "Android 4.2.2", "Android 4.3" };

            listView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.listview_item, R.id.textView1, arr));
        }
}

有趣的部分是您将您的添加AdapterListView. 在这个例子中,我为一个项目添加了我自己的 XML 布局文件,ListView然后我指定了TextView应该用我的字符串数组中的文本填充的内容。

于 2013-10-04T13:34:30.387 回答