implement basic headline loading

This commit is contained in:
Andrew Dolgov 2011-09-09 13:50:15 +04:00
parent 7393ab7631
commit 23e390d2cf
6 changed files with 69 additions and 12 deletions

View File

@ -7,7 +7,9 @@
<ListView android:layout_weight="1" android:background="?headlinesBackground"
android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/headlines"></ListView>
</LinearLayout>
<ProgressBar android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center" android:id="@+id/loading_progress"/>
<TextView android:id="@+id/no_headlines"
android:visibility="invisible"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_headlines"></TextView>
</FrameLayout>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:padding="3dip" android:id="@+id/headlines_row" android:orientation="horizontal">
<CheckBox android:paddingLeft="6dip" android:paddingRight="6dip" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/selected"></CheckBox>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="match_parent">
<TextView android:id="@+id/title" android:textSize="16sp" android:layout_height="wrap_content" android:text="{Title...}" android:layout_width="match_parent"></TextView>
<TextView android:id="@+id/excerpt" android:textSize="13sp" android:layout_height="wrap_content" android:textColor="#909090" android:text="{Content excerpt...}" android:layout_width="match_parent"></TextView>
</LinearLayout>
</LinearLayout>

View File

@ -27,4 +27,5 @@
<string name="offline">Offline</string>
<string name="online">Online</string>
<string name="synchronizing">Synchronizing...</string>
<string name="no_headlines">No articles found.</string>
</resources>

View File

@ -10,7 +10,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
private final String TAG = this.getClass().getSimpleName();
public static final String DATABASE_NAME = "LocalStorage";
public static final int DATABASE_VERSION = 6;
public static final int DATABASE_VERSION = 7;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@ -44,7 +44,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
"link TEXT, " +
"feed_id INTEGER, " +
"tags TEXT, " +
"content TEXT" +
"content TEXT, " +
"excerpt TEXT" +
");");
db.execSQL("CREATE VIEW feeds_unread AS SELECT feeds."+BaseColumns._ID+" AS "+BaseColumns._ID+", " +

View File

@ -3,22 +3,47 @@ package org.fox.ttrss;
import android.app.Activity;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class HeadlinesFragment extends Fragment {
private final String TAG = this.getClass().getSimpleName();
protected int m_feedId;
protected SharedPreferences m_prefs;
protected SQLiteDatabase m_db;
protected Cursor m_cursor;
protected SimpleCursorAdapter m_adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.headlines_fragment, container, false);
DatabaseHelper helper = new DatabaseHelper(getActivity());
m_db = helper.getReadableDatabase();
m_cursor = m_db.query("articles", null, "feed_id = ?", new String[] { String.valueOf(m_feedId) }, null, null, "updated DESC");
m_adapter = new SimpleCursorAdapter(getActivity(), R.layout.headlines_row, m_cursor,
new String[] { "title", "excerpt" }, new int[] { R.id.title, R.id.excerpt }, 0);
ListView list = (ListView) view.findViewById(R.id.headlines);
if (list != null) {
list.setAdapter(m_adapter);
//list.setOnItemClickListener(this);
list.setEmptyView(view.findViewById(R.id.no_headlines));
//list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
}
return view;
}
@ -29,6 +54,14 @@ public class HeadlinesFragment extends Fragment {
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
}
@Override
public void onDestroy() {
super.onDestroy();
m_cursor.close();
m_db.close();
}
public void initialize(int feedId) {
m_feedId = feedId;
}

View File

@ -238,12 +238,12 @@ public class MainActivity extends Activity {
/* db.execSQL("DELETE FROM articles"); */
SQLiteStatement stmtInsert = db.compileStatement("INSERT INTO articles " +
"("+BaseColumns._ID+", unread, marked, published, updated, is_updated, title, link, feed_id, tags, content) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
"("+BaseColumns._ID+", unread, marked, published, updated, is_updated, title, link, feed_id, tags, content, excerpt) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
SQLiteStatement stmtUpdate = db.compileStatement("UPDATE articles SET " +
"unread = ?, marked = ?, published = ?, updated = ?, is_updated = ?, title = ?, link = ?, feed_id = ?, " +
"tags = ?, content = ? WHERE " + BaseColumns._ID + " = ?");
"tags = ?, content = ?, excerpt = ? WHERE " + BaseColumns._ID + " = ?");
for (Article article : articles) {
//Log.d(TAG, "Processing article #" + article.id);
@ -255,6 +255,12 @@ public class MainActivity extends Activity {
Cursor c = db.query("articles", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?",
new String[] { String.valueOf(article.id) }, null, null, null);
String excerpt = article.content.replaceAll("\\<[^>]*>","");
if (excerpt.length() > 120) {
excerpt = excerpt.substring(120) + "...";
}
if (c.getCount() != 0) {
stmtUpdate.bindLong(1, article.unread ? 1 : 0);
stmtUpdate.bindLong(2, article.marked ? 1 : 0);
@ -266,7 +272,8 @@ public class MainActivity extends Activity {
stmtUpdate.bindLong(8, article.feed_id);
stmtUpdate.bindString(9, ""); // comma-separated tags
stmtUpdate.bindString(10, article.content);
stmtUpdate.bindLong(11, article.id);
stmtUpdate.bindString(11, excerpt);
stmtUpdate.bindLong(12, article.id);
stmtUpdate.execute();
} else {
@ -283,8 +290,8 @@ public class MainActivity extends Activity {
stmtInsert.bindLong(9, article.feed_id);
stmtInsert.bindString(10, ""); // comma-separated tags
stmtInsert.bindString(11, article.content);
stmtInsert.execute();
stmtInsert.bindString(12, excerpt);
stmtInsert.execute();
}
c.close();