在Android中将XML解析为SQLite数据库

时间:2011-10-13 17:25:23

标签: android sqlite xml-parsing

这是我的第一个Android项目。我应该从互联网上解析rss feed并将其放入SQLite数据库,以便离线访问feed的帖子。我跟随编码绿色机器人的xml parsing tutorial,我在代码项目上跟随了SQLite database tutorial,但是在尝试将它们放在一起时,我遇到了问题。

该应用程序构建在我的Droid上,但在MainActivity中的此方法中意外停止:

private void displayPosts(ArrayList<Post> posts) {

    //Create String Arrays to separate titles and dates
    Log.d("CGRParser", "Displaying Post Titles To User");
    ArrayList<String> post_headlines = new ArrayList<String>();
    ArrayList<String> post_pubDates = new ArrayList<String>();
    ArrayList<String> post_dbids = new ArrayList<String>();
    ArrayList<String> post_contents = new ArrayList<String>();

    //For every post in the ArrayList posts, it puts each attribute of the post into its own ArrayList
        // This should be getting stuff from the database and putting it into an ArrayList
    for (Post post : posts) {
        Log.d("CGRParser", "Post Title: " + post.getHeadline());
        post_headlines.add(post.getHeadline());
        post_pubDates.add(post.getPubDate());
        post_dbids.add(post.getDbid());
        post_contents.add(post.getContents());
            //it's breaking here
        dbh.AddPost(post);
    }

    this.post_headlines = post_headlines;
    this.post_pubDates = post_pubDates;
    this.post_dbids = post_dbids;
    this.post_contents = post_contents;

    //Create a ListAdapter to Display the Titles in the ListView
    ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.episode_row, R.id.title, post_headlines);
    listview_posts.setAdapter(adapter);

    //Set Progress Bar Invisible since we are done with it
    progress_bar.setVisibility(ProgressBar.INVISIBLE);

}

我保留了原始教程的功能。我改变的只是添加dbh.AddPost(post);。 dbh是MainActivity中声明的DatabaseHelper对象。每个ArrayLists都在MainActivity中声明,但它们也在方法中声明。当我调试它并且它到达dbh.AddPost(post);时,ThreadPoolExecutor.class运行并说“找不到源.JAR文件/Users/Zach/Desktop/android-sdj-mac_x86/platforms/android-7/android.jar有没有源附件。您可以通过单击下面的附加源附加源。“

我的主要问题是我在这里做错了什么?我没有从中得到任何错误,我不明白源附件的含义。我觉得在方法和MainActivity中声明ArrayLists是有原因的,并且我应该对dbh做同样的事情,但我不确定为什么。无论如何,从长远来看,在方法中创建databaseHelper是行不通的,因为应用程序需要从feed中引入新帖子,并且将旧数据库设置为只有新帖子的新数据库将无效。 / p>

编辑:这是DatabaseHelper中的AddPost方法:

void AddPost(Post post){
    SQLiteDatabase db= this.getWritableDatabase();

    ContentValues cv=new ContentValues();
    cv.put(colDbid, post.getDbid());
    cv.put(colType, post.getType());
    cv.put(colSubType, post.getSubType());
    cv.put(colHeadline, post.getHeadline());
    cv.put(colContents, post.getContents());
    cv.put(colUrl, post.getUrl());
    cv.put(colImgUrl, post.getImgUrl());
    cv.put(colVidUrl, post.getVidUrl());
    cv.put(colPubDate, post.getPubDate());

    db.insert(postTable, colHeadline, cv);
    db.close();
}

0 个答案:

没有答案