简体   繁体   English

ANDROID SAX解析器问题

[英]ANDROID SAX Parser issue

Since I am new to java programming, I need a bit of help with this. 由于我是Java编程的新手,因此我需要一些帮助。 I stuck on this one issue and can't continue until I get this to work. 我停留在这个问题上,直到我解决这个问题后才能继续。

I am trying to make a string from that includes a preference int. 我试图从中包括首选项int的字符串。 I saved the data and can display the int (just sample code): 我保存了数据并可以显示int(仅示例代码):

SharedPreferences prefs=PreferenceManager
        .getDefaultSharedPreferences(this);

    list.setText(prefs.getString("list", "22"));

now, I have a xml parser that is pulling a url correctly as a static string: 现在,我有一个XML解析器,它以静态字符串的形式正确提取URL:

public static String feedUrl = String.format("http://www.freshpointmarketing.com/iphone/objects/XML/AND.php?ID=%d", 22);

Works great... 效果很好...

now my issue...... 现在我的问题...

I need to have the preference "int" become the variable in the string, thus making it not static. 我需要使首选项“ int”成为字符串中的变量,从而使其不是静态的。

static SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this);

static int myVariable = prefs.getInt("list1", 22);

public static String feedUrl = String.format("http://www.freshpointmarketing.com/iphone/objects/XML/AND.php?ID=%d", myVariable);

If I take out all static references, I get an error on this: 如果我删除所有静态引用,则会收到以下错误消息:

    private void loadFeed(ParserType type){
    try{
        FeedParser parser = FeedParserFactory.getParser(type);
        long start = System.currentTimeMillis();
        messages = parser.parse();
        long duration = System.currentTimeMillis() - start;
        Log.i("AndroidNews", "Parser duration=" + duration);
        String xml = writeXml();
        Log.i("AndroidNews", xml);
        List<String> titles = new ArrayList<String>(messages.size());
        for (Message msg : messages){
            titles.add(msg.getTitle());
        }
        ArrayAdapter<String> adapter = 
            new ArrayAdapter<String>(this, R.layout.row,titles);
        this.setListAdapter(adapter);
    } catch (Throwable t){
        Log.e("AndroidNews",t.getMessage(),t);
    }
}

thanks 谢谢

Your use of statics here is pretty inappropriate. 您在这里使用静态是非常不合适的。 Better to do this within your Activity lifecycle, such as in onCreate. 最好在Activity生命周期(例如onCreate)中执行此操作。 It's a little hard to see what's going on with the code you posted, but try passing down the url through the application to the place you actually use it, and get rid of all the statics. 很难看清所发布代码的状态,但是请尝试将URL向下通过应用程序传递到实际使用它的位置,并消除所有静态信息。

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

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