好的,所以我试图在Android中使用ListView创建一个RSS应用程序。我有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";
}
}
和
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;
}
}
我用Java测试了java类,它们工作得很好,它返回一个包含标题,描述和链接的10个故事的数组。我的问题是我现在需要使用此数据字段在我的Android应用程序中填充ListView。我在将信息连接到ListView时遇到问题。我试过一个适配器,但我并不是很了解适配器。任何指针都将非常感激。
我尝试过的片段就是这个
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;
}
}
答案 0 :(得分:0)
如果您使用的是ListView,则需要一个适配器。适配器位于ListView和数据之间。
您需要创建一个继承自Adapter的新Adapter类。在它的构造函数中,您传递了NewsStory的数组。然后,在getView()中,您将实例化ListView行布局,并将NewsStory对应的所有数据填入该位置。
这是适配器的基本功能。您可以了解详情并查看示例here。
一旦了解了适配器的工作原理,我强烈建议您将ListView切换到RecyclerView并开始使用ViewHolder模式,这对列表性能和RAM使用有很大帮助。