From 42f35cb3d95e37ec80f291e839abdbae93640683 Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 28 Jan 2014 07:48:08 -0800 Subject: [PATCH] populate study list, doing the sorting in the DB query for now --- .../android/XWords4/res/layout/studylist.xml | 12 ++-- .../src/org/eehouse/android/xw4/DBUtils.java | 27 +++++++++ .../org/eehouse/android/xw4/StudyList.java | 57 +++++++++++++++---- 3 files changed, 79 insertions(+), 17 deletions(-) diff --git a/xwords4/android/XWords4/res/layout/studylist.xml b/xwords4/android/XWords4/res/layout/studylist.xml index e45e783b0..daab666c8 100644 --- a/xwords4/android/XWords4/res/layout/studylist.xml +++ b/xwords4/android/XWords4/res/layout/studylist.xml @@ -15,11 +15,11 @@ android:drawSelectorOnTop="true" /> - + diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java index c316a52e0..efb55c434 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -1607,6 +1607,33 @@ public class DBUtils { return result; } + public static String[] studyListWords( Context context, int lang ) + { + String[] result = null; + String selection = String.format( "%s = %d", DBHelper.LANGUAGE, lang ); + String[] columns = { DBHelper.WORD }; + String orderBy = DBHelper.WORD; + + initDB( context ); + synchronized( s_dbHelper ) { + SQLiteDatabase db = s_dbHelper.getReadableDatabase(); + Cursor cursor = db.query( DBHelper.TABLE_NAME_STUDYLIST, columns, + selection, null, null, null, orderBy ); + int count = cursor.getCount(); + result = new String[count]; + if ( 0 < count ) { + int index = 0; + int colIndex = cursor.getColumnIndex( DBHelper.WORD ); + while ( cursor.moveToNext() ) { + result[index++] = cursor.getString(colIndex); + } + } + cursor.close(); + db.close(); + } + return result; + } + private static void copyGameDB( Context context, boolean toSDCard ) { String name = DBHelper.getDBName(); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/StudyList.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/StudyList.java index 72488cf85..dd146333b 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/StudyList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/StudyList.java @@ -25,9 +25,14 @@ import android.content.Intent; import android.os.Bundle; import android.widget.Spinner; import android.widget.ArrayAdapter; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemSelectedListener; -public class StudyList extends XWListActivity { - private Spinner mSpinner; +public class StudyList extends XWListActivity + implements OnItemSelectedListener { + + private Spinner m_spinner; + private int[] m_langCodes; @Override protected void onCreate( Bundle savedInstanceState ) @@ -36,17 +41,18 @@ public class StudyList extends XWListActivity { setContentView( R.layout.studylist ); - mSpinner = (Spinner)findViewById( R.id.pick_language ); - int[] langs = DBUtils.studyListLangs( this ); - if ( 0 == langs.length ) { + m_spinner = (Spinner)findViewById( R.id.pick_language ); + m_langCodes = DBUtils.studyListLangs( this ); + if ( 0 == m_langCodes.length ) { finish(); - } else if ( 1 == langs.length ) { - mSpinner.setVisibility( View.GONE ); + } else if ( 1 == m_langCodes.length ) { + m_spinner.setVisibility( View.GONE ); + loadList( m_langCodes[0] ); } else { String[] names = DictLangCache.getLangNames( this ); - String[] myNames = new String[langs.length]; - for ( int ii = 0; ii < langs.length; ++ii ) { - myNames[ii] = names[langs[ii]]; + String[] myNames = new String[m_langCodes.length]; + for ( int ii = 0; ii < m_langCodes.length; ++ii ) { + myNames[ii] = names[m_langCodes[ii]]; } ArrayAdapter adapter = new @@ -55,13 +61,42 @@ public class StudyList extends XWListActivity { myNames ); adapter.setDropDownViewResource( android.R.layout. simple_spinner_dropdown_item ); - mSpinner.setAdapter( adapter ); + m_spinner.setAdapter( adapter ); + m_spinner.setOnItemSelectedListener( this ); } } + ////////////////////////////////////////////////// + // AdapterView.OnItemSelectedListener interface + ////////////////////////////////////////////////// + public void onItemSelected( AdapterView parent, View view, + int position, long id ) + { + int lang = m_langCodes[position]; + loadList( lang ); + } + + public void onNothingSelected( AdapterView parent ) + { + } + + private void loadList( int lang ) + { + String[] words = DBUtils.studyListWords( this, lang ); + ArrayAdapter adapter = new ArrayAdapter + ( this, android.R.layout.simple_list_item_1 ); + for ( String word : words ) { + adapter.add( word ); + } + // adapter.sort(); + + setListAdapter( adapter ); + } + public static void launch( Context context ) { Intent intent = new Intent( context, StudyList.class ); context.startActivity( intent ); } + }