Fixed layout and bugs in DBAdapter

This commit is contained in:
Daniel Oeh 2012-04-11 18:14:43 +02:00
parent ba7759607d
commit 7fbd960310
5 changed files with 42 additions and 27 deletions

View File

@ -4,16 +4,16 @@
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imgvFeedimage"
android:layout_height="fill_parent"
android:layout_width="50dip"
android:padding="10dip"
android:layout_height="60dip"
android:layout_width="60dip"
android:layout_marginBottom="1dip"
android:cropToPadding="true"
android:layout_alignParentLeft="true"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_alignParentRight="true">
android:layout_toRightOf="@id/imgvFeedimage">
<TextView
android:id="@+id/txtvFeedname"
android:layout_width="fill_parent"
@ -31,4 +31,4 @@
</RelativeLayout>
</RelativeLayout>

View File

@ -1,6 +1,7 @@
package de.podfetcher;
import de.podfetcher.activity.PodfetcherActivity;
import de.podfetcher.feed.FeedManager;
import android.app.Application;
public class PodcastApp extends Application {
@ -17,8 +18,8 @@ public class PodcastApp extends Application {
super.onCreate();
singleton = this;
//FeedManager manager = FeedManager.getInstance();
//manager.loadDBData(getApplicationContext());
FeedManager manager = FeedManager.getInstance();
manager.loadDBData(getApplicationContext());
}

View File

@ -22,7 +22,6 @@ public class FeedManager {
private ArrayList<Feed> feeds;
private ArrayList<FeedCategory> categories;
Cursor feedlistCursor;
private FeedManager() {
@ -150,14 +149,11 @@ public class FeedManager {
/** Reads the database */
public void loadDBData(Context context) {
PodDBAdapter adapter = new PodDBAdapter(context);
feedlistCursor = adapter.getAllFeedsCursor();
updateArrays(context);
}
public void updateArrays(Context context) {
feedlistCursor.requery();
PodDBAdapter adapter = new PodDBAdapter(context);
feeds.clear();
categories.clear();
extractFeedlistFromCursor(context);
@ -165,6 +161,8 @@ public class FeedManager {
private void extractFeedlistFromCursor(Context context) {
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
Cursor feedlistCursor = adapter.getAllFeedsCursor();
if(feedlistCursor.moveToFirst()) {
do {
Feed feed = new Feed();
@ -173,7 +171,7 @@ public class FeedManager {
feed.setTitle(feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_TITLE)));
feed.setLink(feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_LINK)));
feed.setDescription(feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_DESCRIPTION)));
feed.setImage(adapter.getFeedImage(feed));
feed.setImage(adapter.getFeedImage(feedlistCursor.getLong(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_IMAGE))));
feed.file_url = feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_FILE_URL));
feed.download_url = feedlistCursor.getString(feedlistCursor.getColumnIndex(PodDBAdapter.KEY_DOWNLOAD_URL));
@ -184,12 +182,13 @@ public class FeedManager {
feeds.add(feed);
}while(feedlistCursor.moveToNext());
}
adapter.close();
}
private ArrayList<FeedItem> extractFeedItemsFromCursor(Context context, Cursor itemlistCursor) {
ArrayList<FeedItem> items = new ArrayList<FeedItem>();
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
if(itemlistCursor.moveToFirst()) {
do {
FeedItem item = new FeedItem();
@ -205,6 +204,7 @@ public class FeedManager {
items.add(item);
} while(itemlistCursor.moveToNext());
}
adapter.close();
return items;
}

View File

@ -48,7 +48,7 @@ public class FeedlistAdapter extends ArrayAdapter<Feed> {
TextView txtvFeedname = (TextView)feedlistView.findViewById(R.id.txtvFeedname);
TextView txtvNewEpisodes = (TextView)feedlistView.findViewById(R.id.txtvNewEpisodes);
if(feed.getImage() != null) {
imageView.setImageURI(Uri.fromFile(new File(feed.getFile_url()))); // TODO select default picture when no image downloaded
imageView.setImageURI(Uri.fromFile(new File(feed.getImage().getFile_url()))); // TODO select default picture when no image downloaded
}
txtvFeedname.setText(feed.getTitle());
// TODO find new Episodes txtvNewEpisodes.setText(feed)

View File

@ -237,51 +237,65 @@ public class PodDBAdapter {
}
public Cursor getAllCategoriesCursor() {
return db.query(TABLE_NAME_FEED_CATEGORIES, null, null, null, null, null, null);
open();
Cursor c = db.query(TABLE_NAME_FEED_CATEGORIES, null, null, null, null, null, null);
return c;
}
public Cursor getAllFeedsCursor() {
return db.query(TABLE_NAME_FEEDS, null, null, null, null, null, null);
open();
Cursor c = db.query(TABLE_NAME_FEEDS, null, null, null, null, null, null);
return c;
}
public Cursor getAllItemsOfFeedCursor(Feed feed) {
return db.query(TABLE_NAME_FEED_ITEMS, null, KEY_FEED+"=?", new String[]{String.valueOf(feed.getId())}, null, null, null);
open();
Cursor c = db.query(TABLE_NAME_FEED_ITEMS, null, KEY_FEED+"=?", new String[]{String.valueOf(feed.getId())}, null, null, null);
return c;
}
public Cursor getFeedMediaOfItemCursor(FeedItem item) {
return db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID+"=?", new String[]{String.valueOf(item.getMedia().getId())}, null, null, null);
open();
Cursor c = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID+"=?", new String[]{String.valueOf(item.getMedia().getId())}, null, null, null);
return c;
}
public Cursor getImageOfFeedCursor(Feed feed) {
return db.query(TABLE_NAME_FEED_IMAGES, null, KEY_ID+"=?", new String[]{String.valueOf(feed.getImage().getId())}, null, null, null);
public Cursor getImageOfFeedCursor(long id) {
open();
Cursor c = db.query(TABLE_NAME_FEED_IMAGES, null, KEY_ID+"=?", new String[]{String.valueOf(id)}, null, null, null);
return c;
}
public FeedMedia getFeedMedia(long row_index) throws SQLException{
open();
Cursor cursor = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID+"=?", new String[]{String.valueOf(row_index)}, null, null, null);
if((cursor.getCount() == 0) || !cursor.moveToFirst()) {
throw new SQLException("No FeedMedia found at index: "+ row_index);
}
return new FeedMedia(row_index,
FeedMedia media = new FeedMedia(row_index,
cursor.getLong(cursor.getColumnIndex(KEY_LENGTH)),
cursor.getLong(cursor.getColumnIndex(KEY_POSITION)),
cursor.getLong(cursor.getColumnIndex(KEY_SIZE)),
cursor.getString(cursor.getColumnIndex(KEY_MIME_TYPE)),
cursor.getString(cursor.getColumnIndex(KEY_FILE_URL)),
cursor.getString(cursor.getColumnIndex(KEY_DOWNLOAD_URL)));
close();
return media;
}
public FeedImage getFeedImage(Feed feed) throws SQLException {
Cursor cursor = this.getImageOfFeedCursor(feed);
public FeedImage getFeedImage(long id) throws SQLException {
open();
Cursor cursor = this.getImageOfFeedCursor(id);
if((cursor.getCount() == 0) || !cursor.moveToFirst()) {
throw new SQLException("No FeedImage found at index: "+ feed.getImage().getId());
throw new SQLException("No FeedImage found at index: "+ id);
}
return new FeedImage(feed.getImage().getId(), cursor.getString(cursor.getColumnIndex(KEY_TITLE)),
FeedImage image = new FeedImage(id, cursor.getString(cursor.getColumnIndex(KEY_TITLE)),
cursor.getString(cursor.getColumnIndex(KEY_FILE_URL)),
cursor.getString(cursor.getColumnIndex(KEY_DOWNLOAD_URL)));
close();
return image;
}
private static class PodDBHelper extends SQLiteOpenHelper {