From b50a7c1286437bd6a0e291eeedde1d81eb792e86 Mon Sep 17 00:00:00 2001 From: GAULUPEAU Jonathan Date: Wed, 13 Nov 2013 00:28:42 +0100 Subject: [PATCH] Preparing V2. Sync and Store some articles. --- AndroidManifest.xml | 7 +- res/layout/article_list.xml | 14 ++++ res/layout/list.xml | 14 ++++ res/layout/main.xml | 6 +- res/values/strings.xml | 1 + src/fr/gaulupeau/apps/Poche/Article.java | 18 +++++ .../apps/Poche/ArticlesSQLiteOpenHelper.java | 13 +++ src/fr/gaulupeau/apps/Poche/ListArticles.java | 80 +++++++++++++++++++ src/fr/gaulupeau/apps/Poche/Poche.java | 37 ++++++++- src/fr/gaulupeau/apps/Poche/ReadArticle.java | 45 ++++++++--- .../apps/Poche/ReadingListAdapter.java | 54 +++++++++++++ 11 files changed, 268 insertions(+), 21 deletions(-) create mode 100644 res/layout/article_list.xml create mode 100644 res/layout/list.xml create mode 100644 src/fr/gaulupeau/apps/Poche/Article.java create mode 100644 src/fr/gaulupeau/apps/Poche/ListArticles.java create mode 100644 src/fr/gaulupeau/apps/Poche/ReadingListAdapter.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index e621f87..1c7c489 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,8 +1,7 @@ - + android:versionName="1.0.2" xmlns:android="http://schemas.android.com/apk/res/android"> @@ -22,6 +21,6 @@ - + diff --git a/res/layout/article_list.xml b/res/layout/article_list.xml new file mode 100644 index 0000000..81c74da --- /dev/null +++ b/res/layout/article_list.xml @@ -0,0 +1,14 @@ + + + + + + diff --git a/res/layout/list.xml b/res/layout/list.xml new file mode 100644 index 0000000..3ad5887 --- /dev/null +++ b/res/layout/list.xml @@ -0,0 +1,14 @@ + + + + + + + diff --git a/res/layout/main.xml b/res/layout/main.xml index 6c978a4..c0b07db 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -91,12 +91,12 @@ android:layout_weight="1" android:text="@string/btnGetPost" /> - + android:text="@string/btnSync" /> Your poche URL : Examples:\nhttp://poche.example.fr\nhttp://www.example.fr/poche Save the post + Syncroniser diff --git a/src/fr/gaulupeau/apps/Poche/Article.java b/src/fr/gaulupeau/apps/Poche/Article.java new file mode 100644 index 0000000..8d15d5a --- /dev/null +++ b/src/fr/gaulupeau/apps/Poche/Article.java @@ -0,0 +1,18 @@ +package fr.gaulupeau.apps.Poche; + +public class Article { + public String url; + public String id; + public String title; + public String content; + public String archive; + + public Article(String url, String id, String title, String content, String archive) { + super(); + this.url = url; + this.id = id; + this.title = title; + this.content = content; + this.archive = archive; + } +} diff --git a/src/fr/gaulupeau/apps/Poche/ArticlesSQLiteOpenHelper.java b/src/fr/gaulupeau/apps/Poche/ArticlesSQLiteOpenHelper.java index 3c51709..e759796 100644 --- a/src/fr/gaulupeau/apps/Poche/ArticlesSQLiteOpenHelper.java +++ b/src/fr/gaulupeau/apps/Poche/ArticlesSQLiteOpenHelper.java @@ -20,6 +20,7 @@ public class ArticlesSQLiteOpenHelper extends SQLiteOpenHelper { public static String ARTICLE_CONTENT = "content"; public static String ARTICLE_TITLE = "title"; public static String ARTICLE_URL = "url"; + public static String ARCHIVE = "archive"; Context c; public ArticlesSQLiteOpenHelper(Context context) { @@ -51,8 +52,20 @@ public class ArticlesSQLiteOpenHelper extends SQLiteOpenHelper { ARTICLE_TITLE + " text, " + ARTICLE_URL + " text, " + ARTICLE_ID + " integer, " + + ARCHIVE + " integer" + ");" ); + db.execSQL( + "INSERT INTO " + ARTICLE_TABLE + + " (" + + ARTICLE_TITLE + ", " + + ARTICLE_CONTENT + ", " + + ARTICLE_ID + ", " + + ARTICLE_URL + ", " + + ARCHIVE + + ") VALUES (" + + "'Ceci est un test', 'coucou', 1, 'toto', 0);" + ); } diff --git a/src/fr/gaulupeau/apps/Poche/ListArticles.java b/src/fr/gaulupeau/apps/Poche/ListArticles.java new file mode 100644 index 0000000..6c9dc12 --- /dev/null +++ b/src/fr/gaulupeau/apps/Poche/ListArticles.java @@ -0,0 +1,80 @@ +package fr.gaulupeau.apps.Poche; + +import java.util.ArrayList; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.*; +import fr.gaulupeau.apps.InThePoche.R; +import android.app.Activity; +import android.content.Intent; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ListView; + +public class ListArticles extends Activity { + + private ArrayList
readArticlesInfo; + private ListView readList; + private SQLiteDatabase database; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.list); + setupDB(); + setupList(); + } + + public void onResume() { + super.onResume(); + setupList(); + } + + public void onDestroy() { + super.onDestroy(); + database.close(); + } + + public void setupDB() { + ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(this); + database = helper.getWritableDatabase(); + } + + public void setupList() { + readList = (ListView) findViewById(R.id.liste_articles); + readArticlesInfo = new ArrayList
(); + ReadingListAdapter ad = getAdapterQuery(ARCHIVE + "=0", readArticlesInfo); + readList.setAdapter(ad); + + readList.setOnItemClickListener(new AdapterView.OnItemClickListener() { + + public void onItemClick(AdapterView parent, View view, int position, long id) { + Intent i = new Intent(getBaseContext(), ReadArticle.class); + i.putExtra("id", (String) readArticlesInfo.get(position).id); + startActivity(i); + } + + }); + } + + public ReadingListAdapter getAdapterQuery(String filter, ArrayList
articleInfo) { + Log.e("getAdapterQuery", "running query"); + //String url, String domain, String id, String title, String content + String[] getStrColumns = new String[] {ARTICLE_URL, ARTICLE_ID, ARTICLE_TITLE, ARTICLE_CONTENT, ARCHIVE}; + Cursor ac = database.query( + ARTICLE_TABLE, + getStrColumns, + filter, null, null, null, null); + ac.moveToFirst(); + if(!ac.isAfterLast()) { + do { + Article tempArticle = new Article(ac.getString(0),ac.getString(1),ac.getString(2),ac.getString(3),ac.getString(4)); + articleInfo.add(tempArticle); + } while (ac.moveToNext()); + } + ac.close(); + return new ReadingListAdapter(getBaseContext(), articleInfo); + } + +} diff --git a/src/fr/gaulupeau/apps/Poche/Poche.java b/src/fr/gaulupeau/apps/Poche/Poche.java index 38f941a..aa0ddd1 100644 --- a/src/fr/gaulupeau/apps/Poche/Poche.java +++ b/src/fr/gaulupeau/apps/Poche/Poche.java @@ -9,15 +9,17 @@ package fr.gaulupeau.apps.Poche; import fr.gaulupeau.apps.InThePoche.R; -import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl; import java.io.UnsupportedEncodingException; + import org.json.JSONException; import org.json.JSONObject; import android.annotation.TargetApi; import android.app.Activity; +import android.content.ContentValues; import android.content.Intent; import android.content.SharedPreferences; +import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -30,15 +32,23 @@ import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import static fr.gaulupeau.apps.Poche.Helpers.PREFS_NAME; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_TABLE; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_ID; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_URL; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_TITLE; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_CONTENT; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARCHIVE; +import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl; /** * Main activity class */ @TargetApi(Build.VERSION_CODES.FROYO) public class Poche extends Activity { TextView authorSite; - + private SQLiteDatabase database; Button btnDone; Button btnGetPost; + Button btnSync; EditText editPocheUrl; /** Called when the activity is first created. * Will act differently depending on whether sharing or @@ -104,12 +114,33 @@ import static fr.gaulupeau.apps.Poche.Helpers.PREFS_NAME; } }); + btnSync = (Button)findViewById(R.id.btnSync); + btnSync.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + String ret = getInputStreamFromUrl("http://poche.gaulupeau.fr/toto.php"); + try { + JSONObject rootobj = new JSONObject(ret); + ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(getApplicationContext()); + database = helper.getWritableDatabase(); + ContentValues values = new ContentValues(); + values.put(ARTICLE_TITLE, Html.fromHtml(rootobj.getString("titre")).toString()); + values.put(ARTICLE_CONTENT, Html.fromHtml(rootobj.getString("content")).toString()); + values.put(ARTICLE_ID, Html.fromHtml(rootobj.getString("id")).toString()); + values.put(ARTICLE_URL, Html.fromHtml(rootobj.getString("url")).toString()); + values.put(ARCHIVE, 0); + database.insert(ARTICLE_TABLE, null, values); + } catch (JSONException e) { + e.printStackTrace(); + } + } + }); btnGetPost = (Button)findViewById(R.id.btnGetPost); btnGetPost.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - startActivity(new Intent(getBaseContext(), ReadArticle.class)); + startActivity(new Intent(getBaseContext(), ListArticles.class)); } }); diff --git a/src/fr/gaulupeau/apps/Poche/ReadArticle.java b/src/fr/gaulupeau/apps/Poche/ReadArticle.java index 5da6fc0..454d5d3 100644 --- a/src/fr/gaulupeau/apps/Poche/ReadArticle.java +++ b/src/fr/gaulupeau/apps/Poche/ReadArticle.java @@ -1,11 +1,19 @@ package fr.gaulupeau.apps.Poche; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARCHIVE; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_CONTENT; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_ID; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_TITLE; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_URL; +import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_TABLE; import static fr.gaulupeau.apps.Poche.Helpers.getInputStreamFromUrl; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.text.Html; import android.widget.TextView; @@ -14,22 +22,37 @@ import fr.gaulupeau.apps.InThePoche.R; public class ReadArticle extends Activity { TextView txtTitre; TextView txtContent; + SQLiteDatabase database; + String id = ""; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.article); - String ret = getInputStreamFromUrl("http://poche.gaulupeau.fr/toto.php"); - try { - JSONObject rootobj = new JSONObject(ret); - System.out.println(rootobj); - txtTitre = (TextView)findViewById(R.id.txtTitre); - txtContent = (TextView)findViewById(R.id.txtContent); - txtTitre.setText(Html.fromHtml(rootobj.getString("titre"))); - txtContent.setText(Html.fromHtml(rootobj.getString("content"))); - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(getApplicationContext()); + database = helper.getWritableDatabase(); + String[] getStrColumns = new String[] {ARTICLE_URL, ARTICLE_ID, ARTICLE_TITLE, ARTICLE_CONTENT, ARCHIVE}; + Bundle data = getIntent().getExtras(); + if(data != null) { + id = data.getString("id"); } + Cursor ac = database.query(ARTICLE_TABLE, getStrColumns, ARTICLE_ID + "=" + id, null, null, null, null); + ac.moveToFirst(); + txtTitre = (TextView)findViewById(R.id.txtTitre); + txtTitre.setText(ac.getString(2)); + txtContent = (TextView)findViewById(R.id.txtContent); + txtContent.setText(ac.getString(3)); +// String ret = getInputStreamFromUrl("http://poche.gaulupeau.fr/toto.php"); +// try { +// JSONObject rootobj = new JSONObject(ret); +// System.out.println(rootobj); +// txtTitre = (TextView)findViewById(R.id.txtTitre); +// txtContent = (TextView)findViewById(R.id.txtContent); +// txtTitre.setText(Html.fromHtml(rootobj.getString("titre"))); +// txtContent.setText(Html.fromHtml(rootobj.getString("content"))); +// } catch (JSONException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } } diff --git a/src/fr/gaulupeau/apps/Poche/ReadingListAdapter.java b/src/fr/gaulupeau/apps/Poche/ReadingListAdapter.java new file mode 100644 index 0000000..41c7621 --- /dev/null +++ b/src/fr/gaulupeau/apps/Poche/ReadingListAdapter.java @@ -0,0 +1,54 @@ +package fr.gaulupeau.apps.Poche; + +import java.util.List; + +import fr.gaulupeau.apps.InThePoche.R; + +import android.content.Context; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.TextView; + +public class ReadingListAdapter extends BaseAdapter { + private Context context; + private List
listArticles; + + public ReadingListAdapter(Context context, List
listArticles) { + this.context = context; + this.listArticles = listArticles; + } + + + public int getCount() { + return listArticles.size(); + } + + public Object getItem(int position) { + return listArticles.get(position); + } + + public long getItemId(int position) { + return position; + } + + public View getView(int position, View convertView, ViewGroup parent) { + Article entry = listArticles.get(position); + if (convertView == null) { + LayoutInflater inflater = (LayoutInflater) context + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + convertView = inflater.inflate(R.layout.article_list, null); + } + TextView tvTitle = (TextView) convertView.findViewById(R.id.listitem_titre); + Log.e("title", entry.title); + tvTitle.setText(entry.title); + + return convertView; + } + + + + +}