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;
import android.annotation.TargetApi;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import android.widget.SimpleCursorAdapter;
import fr.gaulupeau.apps.InThePoche.R;
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_TABLE;
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 {
private ArrayList<Article> readArticlesInfo;
private SQLiteDatabase database;
private ListView readList;
private boolean showAll = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
setupDB();
setupList(false);
readList = (ListView) findViewById(R.id.liste_articles);
ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(this);
database = helper.getWritableDatabase();
updateList();
}
public void onDestroy() {
@Override
protected void onResume() {
super.onResume();
updateList();
}
public void onDestroy() {
super.onDestroy();
database.close();
}
@ -51,63 +63,71 @@ public class ListArticles extends BaseActionBarActivity {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuShowAll:
setupList(true);
showAll = !showAll;
updateList();
return true;
case R.id.menuWipeDb:
ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(this);
helper.truncateTables(database);
setupList(false);
updateList();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void setupDB() {
ArticlesSQLiteOpenHelper helper = new ArticlesSQLiteOpenHelper(this);
database = helper.getWritableDatabase();
}
private void updateList() {
CursorAdapter adapter = (CursorAdapter) readList.getAdapter();
if (adapter != null) {
adapter.changeCursor(getCursor());
} else {
setupListAdapter();
}
setTitle("wallabag | " + readList.getCount() + " Articles");
}
public void setupList(Boolean showAll) {
ListView readList = (ListView) findViewById(R.id.liste_articles);
readArticlesInfo = new ArrayList<Article>();
String filter = null;
if (!showAll) {
filter = ARCHIVE + "=0";
}
ReadingListAdapter ad = getAdapterQuery(filter, readArticlesInfo);
readList.setAdapter(ad);
private void setupListAdapter() {
CursorAdapter adapter = getCursorAdapter();
readList.setAdapter(adapter);
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", 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);
}
});
setTitle("wallabag | " + readArticlesInfo.size() + " Articles");
}
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, MY_ID, ARTICLE_TITLE, ARTICLE_CONTENT, ARCHIVE};
Cursor ac = database.query(
ARTICLE_TABLE,
getStrColumns,
filter, null, null, null, ARTICLE_DATE + " DESC");
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);
}
private Cursor getCursor() {
String filter = null;
if (!showAll) {
filter = ARCHIVE + "=0";
}
// the " as _id" extension is important, as a CursorAdapter needs a column named '_id' to work
// with this extension we get something starting like "Select id as _id,"...
String[] columns = new String[]{MY_ID + " as _id", ARTICLE_TITLE, ARTICLE_URL};
return database.query(
ARTICLE_TABLE,
columns,
filter, null, null, null, ARTICLE_DATE + " DESC");
}
@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};
Bundle data = getIntent().getExtras();
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);
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;
}
}