0

以下是我的代码。它显示获取的 RSS 标题、日期和描述。我想要做的是,当用户单击标题时,它必须下拉带有图像的描述。怎么做?我被困住了。我做了很多搜索。它还有另一个问题是它在元素之间显示文本“obj”。

这是我的应用程序的快照

RSSItem.java 



public class RSSItem {
    public String title;
    public String date;
    public String link;
    public String description;
}

RSSListActivities.java

    package com.varma.samples.rssreader.ui;

    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;

    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;

    import android.app.ListActivity;
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;

    import com.varma.samples.rssreader.R;
    import com.varma.samples.rssreader.data.RSSItem;
    import com.varma.samples.rssreader.xmlparser.RSSParser;

    public class RSSListActivity extends ListActivity {
    private ArrayList<RSSItem> itemlist = null;
    private RSSListAdaptor rssadaptor = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        itemlist = new ArrayList<RSSItem>();

        new RetrieveRSSFeeds().execute();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        RSSItem data = itemlist.get(position);

        Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(data.link));

        startActivity(intent);
    }

    private void retrieveRSSFeed(String urlToRssFeed,ArrayList<RSSItem> list)
    {
        try
        {
           URL url = new URL(urlToRssFeed);
           SAXParserFactory factory = SAXParserFactory.newInstance();
           SAXParser parser = factory.newSAXParser();
           XMLReader xmlreader = parser.getXMLReader();
           RSSParser theRssHandler = new RSSParser(list);

           xmlreader.setContentHandler(theRssHandler);

           InputSource is = new InputSource(url.openStream());

           xmlreader.parse(is);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private class RetrieveRSSFeeds extends AsyncTask<Void, Void, Void>
    {
        private ProgressDialog progress = null;

        @Override
        protected Void doInBackground(Void... params) {
            retrieveRSSFeed("RSS-URL",itemlist);

            rssadaptor = new RSSListAdaptor(RSSListActivity.this, R.layout.rssitemview,itemlist);

            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(
                    RSSListActivity.this, null, "Loading RSS Feeds...");

            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void result) {
            setListAdapter(rssadaptor);

            progress.dismiss();

            super.onPostExecute(result);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }

    private class RSSListAdaptor extends ArrayAdapter<RSSItem>{
        private List<RSSItem> objects = null;

        public RSSListAdaptor(Context context, int textviewid, List<RSSItem> objects) {
            super(context, textviewid, objects);

            this.objects = objects;
        }

        @Override
        public int getCount() {
            return ((null != objects) ? objects.size() : 0);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public RSSItem getItem(int position) {
            return ((null != objects) ? objects.get(position) : null);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;

            if(null == view)
            {
                LayoutInflater vi = (LayoutInflater)RSSListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.rssitemview, null);
            }

            RSSItem data = objects.get(position);

            if(null != data)
            {
                TextView title = (TextView)view.findViewById(R.id.txtTitle);
                TextView date = (TextView)view.findViewById(R.id.txtDate);
                TextView description = (TextView)view.findViewById(R.id.txtDescription);

                title.setText(android.text.Html.fromHtml(data.title).toString());
                date.setText("on " + android.text.Html.fromHtml(data.date).toString());
                description.setText(android.text.Html.fromHtml(data.description).toString());
            }

            return view;
        }
    }

   }

RSSParser.java

package com.varma.samples.rssreader.xmlparser;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.varma.samples.rssreader.data.RSSItem;

public class RSSParser extends DefaultHandler {
private final static String TAG_ITEM = "item";
private final static String[] xmltags = { "title", "link", "pubDate", "description" };

private RSSItem currentitem = null;
private ArrayList<RSSItem> itemarray = null;
private int currentindex = -1;
private boolean isParsing = false;
private StringBuilder builder = new StringBuilder();

public RSSParser(ArrayList<RSSItem> itemarray) {
    super();

    this.itemarray = itemarray;
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    super.characters(ch, start, length);

    if(isParsing && -1 != currentindex && null != builder)
    {
        builder.append(ch,start,length);
    }
}

@Override
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
    super.startElement(uri, localName, qName, attributes);

    if(localName.equalsIgnoreCase(TAG_ITEM))
    {
        currentitem = new RSSItem();
        currentindex = -1;
        isParsing = true;

        itemarray.add(currentitem);
    }
    else
    {
        currentindex = itemIndexFromString(localName);

        builder = null;

        if(-1 != currentindex)
            builder = new StringBuilder();
    }
}

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

    if(localName.equalsIgnoreCase(TAG_ITEM))
    {
        isParsing = false;
    }
    else if(currentindex != -1)
    {
        if(isParsing)
        {
            switch(currentindex)
            {
                case 0: currentitem.title = builder.toString();         break; 
                case 1: currentitem.link = builder.toString();          break;
                case 2: currentitem.date = builder.toString();          break;
                case 3: currentitem.description= builder.toString();    break;
            }
        }
    }
}

private int itemIndexFromString(String tagname){
    int itemindex = -1;

    for(int index= 0; index<xmltags.length; ++index)
    {
        if(tagname.equalsIgnoreCase(xmltags[index]))
        {
            itemindex = index;

            break;
        }
    }

    return itemindex;
}
}

主要的.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">

    <ListView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:id="@android:id/list"/>
</LinearLayout>

rssitemview.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="wrap_content"
    android:padding="10dip">

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="20dip"
        android:padding="5dip"
        android:text="Title"/>

    <TextView
        android:id="@+id/txtDate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:textSize="10dip"
        android:padding="5dip"
        android:text="Date"/>

    <TextView
        android:id="@+id/txtDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="normal"
        android:textSize="10dip"
        android:padding="5dip"
        android:text="Description"/>

</LinearLayout>
4

2 回答 2

0

您可以使用以下方式完成它(我将只提供必要的代码,因为 RSSParser 和图像加载例如显然超出了这个问题的范围):

public class RSSListActivity extends ListActivity {
    private ArrayList<RSSItem> itemlist = null;
    private RSSListAdaptor rssadaptor = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rss_activity);

        itemlist = new ArrayList<RSSItem>();

        // Put hardcoded items temporary
        for(int i = 0; i < 25; i++) {
            itemlist.add(new RSSItem("RSS item " + i,
                    "2013/03/" + i,
                    "http://www.northlight-images.co.uk/gallery_images/pics/hood_canal.jpg",
                    "Description of RSS test image #" + i + "Description of RSS test image #" + i  + "Description of RSS test image #" + i));
        }

        rssadaptor = new RSSListAdaptor(RSSListActivity.this, R.layout.rssitemview,itemlist);

        setListAdapter(rssadaptor);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        RSSItem data = itemlist.get(position);

        Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(data.link));

        startActivity(intent);
    }

    private void retrieveRSSFeed(String urlToRssFeed,ArrayList<RSSItem> list) {
        try {
            URL url = new URL(urlToRssFeed);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader xmlreader = parser.getXMLReader();

            InputSource is = new InputSource(url.openStream());

            xmlreader.parse(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class RSSListAdaptor extends ArrayAdapter<RSSItem>{
        private List<RSSItem> objects = null;

        private int mFocusedPosition = -1;

        private static final int COUNT_ITEM_TYPES = 2;
        private static final int ITEM_TYPE_NORMAL = 0;
        private static final int ITEM_TYPE_EXPANDED = 1;

        public RSSListAdaptor(Context context, int textviewid, List<RSSItem> objects) {
            super(context, textviewid, objects);

            this.objects = objects;
        }

        @Override
        public int getCount() {
            return ((null != objects) ? objects.size() : 0);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public RSSItem getItem(int position) {
            return ((null != objects) ? objects.get(position) : null);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            final int itemType = getItemViewType(position);

            if(null == view) {
                final LayoutInflater vi = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                view = vi.inflate(itemType == ITEM_TYPE_NORMAL ? R.layout.rssitemview : R.layout.rssitemview_expanded, null);
            }

            final RSSItem data = objects.get(position);

            if(null != data) {
                // TODO: ViewHolder pattern should be used here
                final TextView title = (TextView)view.findViewById(R.id.txtTitle);
                final TextView date = (TextView)view.findViewById(R.id.txtDate);
                final TextView description = (TextView)view.findViewById(R.id.txtDescription);
                final ImageView image = (ImageView)view.findViewById(R.id.image);

                if (null != title) {
                    title.setText(android.text.Html.fromHtml(data.title).toString());
                    title.setFocusable(false);

                    final int itemPosition = position;

                    title.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(final View v) {
                            // Clicked view becomes focused
                            mFocusedPosition = itemPosition;
                            notifyDataSetChanged();
                        }
                    });
                }

                if (null != date) {
                    date.setText("on " + android.text.Html.fromHtml(data.date).toString());
                }

                if (null != description) {
                    description.setText(android.text.Html.fromHtml(data.description).toString());
                }

                if (null != image) {
                    image.setImageURI(Uri.parse(data.link));
                }
            }

            return view;
        }

        @Override
        public int getViewTypeCount() {
            return COUNT_ITEM_TYPES;
        }

        @Override
        public int getItemViewType(final int position) {
            return (position == mFocusedPosition) ? ITEM_TYPE_EXPANDED : ITEM_TYPE_NORMAL;
        }
    }

}

这里:

rssitemview.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:padding="10dip">

    <TextView
            android:id="@+id/txtTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="20dip"
            android:padding="5dip"
            android:text="Title" />

</LinearLayout>

和 rssitemview_expanded.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:padding="10dip">

    <TextView
            android:id="@+id/txtTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="20dip"
            android:padding="5dip"
            android:text="Title" />

    <TextView
            android:id="@+id/txtDate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="italic"
            android:textSize="10dip"
            android:padding="5dip"
            android:text="Date" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="30dp"
        android:layout_height="30dp" />

    <TextView
            android:id="@+id/txtDescription"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="normal"
            android:textSize="10dip"
            android:padding="5dip"
            android:text="Description" />

</LinearLayout>

关于第二个问题:“它还有另一个问题是它在元素之间显示文本“obj”。”,请提出另一个问题并提供必要的信息(例如 rss 数据示例)。

于 2013-03-26T05:22:20.030 回答
0

简单的解决方案。最初设置文本视图的可见性 txtTitle 可见 txtDate 不可见 txtDescription 不可见

实现监听器

根据您的要求更改可见性

于 2013-03-26T04:16:04.367 回答