Preparing V2. Sync and Store some articles.

This commit is contained in:
GAULUPEAU Jonathan 2013-11-13 00:28:42 +01:00
parent 7fb608b8d7
commit b50a7c1286
11 changed files with 268 additions and 21 deletions

View File

@ -1,8 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.gaulupeau.apps.InThePoche"
<manifest package="fr.gaulupeau.apps.InThePoche"
android:versionCode="3"
android:versionName="1.0.2">
android:versionName="1.0.2" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
@ -22,6 +21,6 @@
</intent-filter>
</activity>
<activity android:name="fr.gaulupeau.apps.Poche.ReadArticle" android:label="@string/app_name" android:theme="@android:style/Theme.Light"></activity>
<activity android:name="fr.gaulupeau.apps.Poche.ListArticles" android:label="@string/app_name" android:theme="@android:style/Theme.Light"></activity>
</application>
</manifest>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="10sp"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView
android:id="@+id/listitem_titre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="titre"
android:textSize="13sp"
android:typeface="serif" />
</LinearLayout>

14
res/layout/list.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/liste_articles"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>

View File

@ -91,12 +91,12 @@
android:layout_weight="1"
android:text="@string/btnGetPost" />
<TextView
android:id="@+id/txtContent"
<Button
android:id="@+id/btnSync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
android:text="@string/btnSync" />
<TextView
android:id="@+id/author"

View File

@ -11,4 +11,5 @@
<string name="url_label"><b><u>Your poche URL :</u></b></string>
<string name="url_help">Examples:\n<i>http://poche.example.fr</i>\n<i>http://www.example.fr/poche</i></string>
<string name="btnGetPost">Save the post</string>
<string name="btnSync">Syncroniser</string>
</resources>

View File

@ -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;
}
}

View File

@ -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);"
);
}

View File

@ -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<Article> 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<Article>();
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<Article> 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);
}
}

View File

@ -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));
}
});

View File

@ -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();
// }
}

View File

@ -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<Article> listArticles;
public ReadingListAdapter(Context context, List<Article> 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;
}
}