issue #55: using SimpleCursorAdapter instead of own ReadingListAdapter.

This commit is contained in:
Kevin Meyer 2014-12-16 18:00:20 +01:00
parent e9fd003efc
commit f41a17de50
3 changed files with 66 additions and 97 deletions

View File

@ -1,22 +1,23 @@
package fr.gaulupeau.apps.Poche; package fr.gaulupeau.apps.Poche;
import android.annotation.TargetApi;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import java.util.ArrayList;
import fr.gaulupeau.apps.InThePoche.R; import fr.gaulupeau.apps.InThePoche.R;
import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARCHIVE; 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_DATE; import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_DATE;
import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_TABLE; import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_TABLE;
import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_TITLE; import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.ARTICLE_TITLE;
@ -25,17 +26,28 @@ import static fr.gaulupeau.apps.Poche.ArticlesSQLiteOpenHelper.MY_ID;
public class ListArticles extends BaseActionBarActivity { public class ListArticles extends BaseActionBarActivity {
private ArrayList<Article> readArticlesInfo;
private SQLiteDatabase database; private SQLiteDatabase database;
private ListView readList;
private boolean showAll = false;
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.list); setContentView(R.layout.list);
setupDB(); readList = (ListView) findViewById(R.id.liste_articles);
setupList(false);
ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(this);
database = helper.getWritableDatabase();
updateList();
} }
public void onDestroy() { @Override
protected void onResume() {
super.onResume();
updateList();
}
public void onDestroy() {
super.onDestroy(); super.onDestroy();
database.close(); database.close();
} }
@ -51,63 +63,71 @@ public class ListArticles extends BaseActionBarActivity {
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.menuShowAll: case R.id.menuShowAll:
setupList(true); showAll = !showAll;
updateList();
return true; return true;
case R.id.menuWipeDb: case R.id.menuWipeDb:
ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(this); ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(this);
helper.truncateTables(database); helper.truncateTables(database);
setupList(false); updateList();
return true; return true;
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
} }
public void setupDB() { private void updateList() {
ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(this); CursorAdapter adapter = (CursorAdapter) readList.getAdapter();
database = helper.getWritableDatabase(); if (adapter != null) {
} adapter.changeCursor(getCursor());
} else {
setupListAdapter();
}
setTitle("wallabag | " + readList.getCount() + " Articles");
}
public void setupList(Boolean showAll) { private void setupListAdapter() {
ListView readList = (ListView) findViewById(R.id.liste_articles); CursorAdapter adapter = getCursorAdapter();
readArticlesInfo = new ArrayList<Article>(); readList.setAdapter(adapter);
String filter = null;
if (!showAll) {
filter = ARCHIVE + "=0";
}
ReadingListAdapter ad = getAdapterQuery(filter, readArticlesInfo);
readList.setAdapter(ad);
readList.setOnItemClickListener(new AdapterView.OnItemClickListener() { readList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getBaseContext(), ReadArticle.class); Intent i = new Intent(getBaseContext(), ReadArticle.class);
i.putExtra("id", readArticlesInfo.get(position).id); // As we use a CursorAdapter the id's are the same as in our SQLite Database.
i.putExtra("id", id);
startActivity(i); startActivity(i);
} }
}); });
setTitle("wallabag | " + readArticlesInfo.size() + " Articles");
} }
public ReadingListAdapter getAdapterQuery(String filter, ArrayList<Article> articleInfo) { private Cursor getCursor() {
//Log.e("getAdapterQuery", "running query"); String filter = null;
//String url, String domain, String id, String title, String content if (!showAll) {
String[] getStrColumns = new String[]{ARTICLE_URL, MY_ID, ARTICLE_TITLE, ARTICLE_CONTENT, ARCHIVE}; filter = ARCHIVE + "=0";
Cursor ac = database.query( }
ARTICLE_TABLE, // the " as _id" extension is important, as a CursorAdapter needs a column named '_id' to work
getStrColumns, // with this extension we get something starting like "Select id as _id,"...
filter, null, null, null, ARTICLE_DATE + " DESC"); String[] columns = new String[]{MY_ID + " as _id", ARTICLE_TITLE, ARTICLE_URL};
ac.moveToFirst(); return database.query(
if (!ac.isAfterLast()) { ARTICLE_TABLE,
do { columns,
Article tempArticle = new Article(ac.getString(0), ac.getString(1), ac.getString(2), ac.getString(3), ac.getString(4)); filter, null, null, null, ARTICLE_DATE + " DESC");
articleInfo.add(tempArticle); }
} while (ac.moveToNext());
}
ac.close();
return new ReadingListAdapter(getBaseContext(), articleInfo);
}
@TargetApi(11)
private CursorAdapter getCursorAdapter() {
int layout = R.layout.article_list;
String[] columns = new String[]{ARTICLE_TITLE, ARTICLE_URL};
int[] toIds = new int[]{R.id.listitem_titre, R.id.listitem_textview_url};
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
return getCursorAdapterPreHoneycomb(layout, columns, toIds);
}
return new SimpleCursorAdapter(this, layout, getCursor(), columns, toIds, 0);
}
@TargetApi(8)
private CursorAdapter getCursorAdapterPreHoneycomb(int layout, String[] from, int[] to) {
return new SimpleCursorAdapter(this, layout, getCursor(), from, to);
}
} }

View File

@ -40,7 +40,7 @@ public class ReadArticle extends BaseActionBarActivity {
String[] getStrColumns = new String[]{ARTICLE_URL, MY_ID, ARTICLE_TITLE, ARTICLE_CONTENT, ARCHIVE, ARTICLE_AUTHOR}; String[] getStrColumns = new String[]{ARTICLE_URL, MY_ID, ARTICLE_TITLE, ARTICLE_CONTENT, ARCHIVE, ARTICLE_AUTHOR};
Bundle data = getIntent().getExtras(); Bundle data = getIntent().getExtras();
if (data != null) { if (data != null) {
id = data.getString("id"); id = String.valueOf(data.getLong("id"));
} }
Cursor ac = database.query(ARTICLE_TABLE, getStrColumns, MY_ID + "=" + id, null, null, null, null); Cursor ac = database.query(ARTICLE_TABLE, getStrColumns, MY_ID + "=" + id, null, null, null, null);
ac.moveToFirst(); ac.moveToFirst();

View File

@ -1,51 +0,0 @@
package fr.gaulupeau.apps.Poche;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import fr.gaulupeau.apps.InThePoche.R;
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, parent, false);
}
TextView tvTitle = (TextView) convertView.findViewById(R.id.listitem_titre);
TextView tvHost = (TextView) convertView.findViewById(R.id.listitem_textview_url);
tvTitle.setText(entry.title);
tvHost.setText(entry.getHostOfUrl());
return convertView;
}
}