【问题标题】:Java/Android FeedParser - Add one more Menu ItemJava/Android FeedParser - 添加一个菜单项
【发布时间】:2017-05-14 05:54:50
【问题描述】:

已下载此源代码:https://github.com/shikha14/ParsingRssFeeds

我想添加更多菜单项,但它不起作用...

原代码在

AppUtils.java

public class AppUtils {

public static final String RSS_CNN_NEWS = "http://rss.cnn.com/rss/cnn_latest.rss";
public static final String RSS_GOOGLE_NEW = "https://news.google.com/?output=rss";
public static final String TAG = "DIGIPLUSE";

和 Api.java:

 public void getNews(int position, FetchListener listener) {
    String url = null;
    if (position == 0) {
        url = AppUtils.RSS_CNN_NEWS;
    } else {
        url = AppUtils.RSS_GOOGLE_NEW;
    }
    new AsyncHTTPPost().execute(url, listener);
}

我试试这个:

public class AppUtils {

public static final String RSS_1 = "http://example.org/feed&type=rss";
public static final String RSS_2 = "http://example.org/feed&type=rss";
public static final String RSS_3 = "http://example.org/feed&type=rss";
public static final String RSS_4 = "http://example.org/feed&type=rss";

还有这个:

public void getNews(int position, FetchListener listener) {
String url = null;
if (position == 0) {
url = AppUtils.RSS_1;
} else {
url = AppUtils.RSS_2;
url = AppUtils.RSS_3;
url = AppUtils.RSS_4;
}

当我启动应用程序时,它会显示 4 个项目,但 url 仅适用于第一个和第二个菜单项... 有人有想法吗?

感谢您的回答...

【问题讨论】:

    标签: java android xml parsing


    【解决方案1】:

    您的代码在问题中只有两个条件:if (position == 0)else。您需要处理额外的职位。此外,在您的“其他”块中,您将 AppUtils.RSS_2 的值分配给局部变量 url。然后将 AppUtils.RSS_3 分配给 url,它会替换它之前引用的值。然后重复 AppUtils.RSS_4。这不会取得任何成果,因为您的每项作业都覆盖了前一项的效果。

    假设您实际创建了菜单项(您的问题不包含任何相关内容),然后处理它们:

    if (position == 0) {
        url = AppUtils.RSS_1;
    } else if (position == 1) {
        url = AppUtils.RSS_2;
    } else if (position == 2) {
        url = AppUtils.RSS_3;
    } else if (position == 3) {
        url = AppUtils.RSS_4;
    } else {
        throw new IllegalArugmentException("Unrecognized position: "+position);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      • 2023-03-02
      相关资源
      最近更新 更多