简体   繁体   English

RSS应用程序,我的ListView遇到问题

[英]RSS app, having trouble with my ListView

Ok so I'm trying to make an RSS app using ListView in android. 好的,所以我试图使用Android中的ListView制作RSS应用程序。 I have 2 classes that reads the html and returns an array of objects containing the title, description, and link. 我有2个类,它们读取html并返回包含标题,描述和链接的对象数组。

public class RSS
{
    public static NewsStory[] readRSS(String urlAddress)
    {
        try{
            NewsStory[] stories = new NewsStory[10];
            URL rssUrl = new URL(urlAddress);
            BufferedReader in = new BufferedReader(new InputStreamReader(rssUrl.openStream()));
            String rawString;
            String item = "";
            int i = 0;
            while((rawString = in.readLine()) != null)
            {
                if(rawString.contains("<item>"))
                {

                    int titleEndIndex = 0;
                    int titleStartIndex = 0;
                    while (titleStartIndex >= 0 && i < 10)
                    {
                        titleStartIndex = rawString.indexOf("<item>", titleEndIndex);
                        if (titleStartIndex >= 0)
                        {
                            titleEndIndex = rawString.indexOf("</item>", titleStartIndex);
                            item = rawString.substring(titleStartIndex + "<item>".length(), titleEndIndex) + "\n";
                        }
                        stories[i] = new NewsStory(getContent(item,"title"),getContent(item,"description"),getContent(item,"link"));
                        i++;
                    }
                    in.close();
                    return stories;
                }
            }
        }
        catch(MalformedURLException ue)
        {
            System.out.print("Malformed URL");
        }
        catch(IOException ioe)
        {
            System.out.print("Something went wrong reading the contents");
        }
        return null;
    }

    public static String getContent(String item, String target)
    {
        String excerpt = "";
        int titleEndIndex = 0;
        int titleStartIndex = 0;
        while (titleStartIndex >= 0) {
            titleStartIndex = item.indexOf("<"+target+">", titleEndIndex);
            if (titleStartIndex >= 0) {
                if (target.equals("description"))
                    titleEndIndex = item.indexOf("&", titleStartIndex);
                else
                    titleEndIndex = item.indexOf("</"+target+">", titleStartIndex);
                excerpt += item.substring(titleStartIndex + ("<"+target+">").length(), titleEndIndex) + "\n";

            }
            return excerpt;
        }
        return "error";
    }
}

and

 public class NewsStory {

    String title;
    String description;
    String link;


    public NewsStory()
    {
        this.title = "";
        this.description = "";
        this.link = "";
    }
    public NewsStory(String title, String description, String link) {
        this.title = title;
        this.description = description;
        this.link = link;
    }

    public String getTitle() {
        return title;
    }

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

    public String getDescription() {
        return description;
    }

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

    public String getLink() {
        return link;
    }

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

}

I tested the java classes in Java, and they work perfectly, it returns an array with 10 stories containing title, description, and link. 我用Java测试了Java类,它们可以完美工作,它返回一个包含10个故事的数组,其中包含标题,描述和链接。 My problem is that I need to now populate the ListView in my android app using the data fields from this. 我的问题是,我现在需要使用此数据字段在Android应用程序中填充ListView。 I'm having trouble connecting the info to the ListView. 我在将信息连接到ListView时遇到问题。 I tried an adapter, but I don't really understand adapters. 我尝试了适配器,但我不太了解适配器。 Any pointers would be greatly appreciated. 任何指针将不胜感激。

the fragment that I'm trying it in is this 我正在尝试的片段是

public class HeadlineFragment extends Fragment {

    EditText input;
    Button search;
    ListView headlines;
    private NewsDataSource ds;
    private ListView newsListView;

    public HeadlineFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_headline,container,false);
        ds = new NewsDataSource();
        newsListView = (ListView)v.findViewById(R.id.listView);
        //newsListView.setAdapter(new NewsDataSourceAdapter());

        input = (EditText)v.findViewById(R.id.txtInput);
        search = (Button)v.findViewById(R.id.btnSearch);
        headlines = (ListView)v.findViewById(R.id.listView);

        return v;
    }

}

If you're using a ListView, you need an Adapter. 如果您使用的是ListView,则需要一个适配器。 Adapter are in between your ListView and your data. 适配器位于ListView和数据之间。

You need to create a new Adapter class that inherits from Adapter. 您需要创建一个继承自Adapter的新Adapter类。 In its constructor, you pass your array of NewsStory's. 在其构造函数中,您传递NewsStory的数组。 Then, in getView() you instatiate the ListView row layout and fill in all the data from the NewsStory correspoding to that position. 然后,在getView()中,将ListView行布局实例化,并填充来自NewsStory的与该位置相对应的所有数据。

This is the basics functioning of an Adapter. 这是适配器的基本功能。 You can learn more and see examples here . 您可以在此处了解更多信息和示例。

Once you understand how Adapters work, I'd strongly suggest you switch ListView into RecyclerView and start using the ViewHolder pattern, which greatly helps with lists performance and RAM usage. 一旦您了解了适配器的工作原理,我强烈建议您将ListView切换到RecyclerView并开始使用ViewHolder模式,这将大大有助于提高列表性能和RAM使用率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM