Parsing XML into SQLite Database in Android
This is my first Android project. I am supposed to parse an rss feed from the internet and put it into a SQLite database for offline access to the feed's posts. I have followed the xml parsing tutorial by Coding Green Robots and I have followed a SQLite database tutorial on the Code Project, but in trying to put those together, I have run into problems.
The app builds on my Droid, but then it stops unexpectedly during this method in 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);
}
I retained the functionality from the original tutorial. All I changed was adding dbh.AddPost(post);
. dbh is a DatabaseHelper object declared in MainActivity. Each of those ArrayLists are declared in Main开发者_Python百科Activity, but they are also declared in the method. When I debug it and it gets to dbh.AddPost(post);
ThreadPoolExecutor.class runs and says "Source not found. The JAR file /Users/Zach/Desktop/android-sdj-mac_x86/platforms/android-7/android.jar has no source attachment. You can attach the source by clicking Attach Source below."
My main question is what am I doing wrong here? I don't get any errors from this, and I don't understand what it means by source attachment. I feel that there is a reason the ArrayLists are declared in the method as well as MainActivity, and that I should be doing the same for dbh, but I am not sure why. Regardless, making a databaseHelper in the method wouldn't work in the long run, as the application will need to bring in new posts from the feed, and setting the old database to the new one with only the new posts would not work.
EDIT: Here's the AddPost method in DatabaseHelper:
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();
}
精彩评论