好吧,所以我想在android中使用ListView製作一個RSS應用程序。我有2個類讀取html並返回包含標題,描述和鏈接的對象數組。RSS應用程序,與我的ListView有問題
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;
}
}
發佈適配器代碼可能有助於瞭解什麼是錯的 – GSala
請將您的適配器.....! –