Entries limit from preferences

This commit is contained in:
Przemek Grondek 2014-07-21 12:25:54 +02:00
parent 56cd6f109b
commit 5a7aa1797c
2 changed files with 38 additions and 7 deletions

View file

@ -1,6 +1,7 @@
package info.nerull7.mysqlbrowser;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.os.Bundle;
@ -24,6 +25,7 @@ public class EntriesFragment extends Fragment {
TableLayout entriesTable;
ScrollView entriesScrollView;
FrameLayout headerFrame;
private int entriesLimit;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@ -34,6 +36,7 @@ public class EntriesFragment extends Fragment {
entriesTable = (TableLayout) rootView.findViewById(R.id.entriesTable);
entriesScrollView = (ScrollView) rootView.findViewById(R.id.entriesScrollView);
headerFrame = (FrameLayout) rootView.findViewById(R.id.headerFrame);
entriesLimit = getActivity().getSharedPreferences(SettingsFragment.PREFERENCE_FILE, Context.MODE_PRIVATE).getInt(SettingsFragment.ENTRIES_PAGE_LIMIT, SettingsFragment.ENTRIES_PAGE_LIMIT_DEF);
setupTable();
return rootView;
}
@ -75,11 +78,11 @@ public class EntriesFragment extends Fragment {
headerFrame.addView(fakeHeaderView);
// Now we get Rows
List<List<String>> rows = Static.databaseConnector.getRows(tableName, 50); //TODO some normal number definition in header
for(int i=0;i<rows.size();i++){ // The same arbitrary number FIXME
List<List<String>> rows = Static.databaseConnector.getRows(tableName, entriesLimit);
for(int i=0;i<rows.size();i++){
List<String> elements = rows.get(i);
TableRow newRow = new TableRow(getActivity());
for(int j=0;j<elements.size();j++) { // elements.size is the same as in header so maybe some one number ?
for(int j=0;j<elements.size();j++) { // elements.size can be the same as in header so maybe some one number or not
TextView textView = new TextView(getActivity());
textView.setText(elements.get(j));
textView.setLayoutParams(layoutParams);

View file

@ -1,5 +1,8 @@
package info.nerull7.mysqlbrowser;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
@ -8,14 +11,22 @@ import android.preference.PreferenceScreen;
/**
* Created by nerull7 on 18.07.14.
*/
public class SettingsFragment extends PreferenceFragment{
public final static String ENTRIES_PAGE_LIMIT = "entries_limit";
public class SettingsFragment extends PreferenceFragment implements NumberPickerDialog.OnNumberSetListener {
public static final String PREFERENCE_FILE = "preferences";
public static final String ENTRIES_PAGE_LIMIT = "entries_limit";
Preference mEntriesLimit;
public static final int ENTRIES_PAGE_LIMIT_DEF = 20;
public static final int ENTRIES_MIN_PAGE = 20;
public static final int ENTRIES_MAX_PAGE = 100;
private Activity parent;
private SharedPreferences preferences;
private Preference mEntriesLimit;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
preferences = getActivity().getSharedPreferences(PREFERENCE_FILE, Context.MODE_PRIVATE);
loadPrefs();
}
@ -23,14 +34,31 @@ public class SettingsFragment extends PreferenceFragment{
addPreferencesFromResource(R.xml.settings);
mEntriesLimit = findPreference(ENTRIES_PAGE_LIMIT);
setEntriesPageLimitSummary();
}
private int getEntriesPageLimit(){
return preferences.getInt(ENTRIES_PAGE_LIMIT, ENTRIES_PAGE_LIMIT_DEF);
}
private void setEntriesPageLimitSummary(){
mEntriesLimit.setSummary(getString(R.string.entries_summary, getEntriesPageLimit()));
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if(preference == mEntriesLimit){
new NumberPickerDialog
new NumberPickerDialog(getActivity(), this, getEntriesPageLimit(), ENTRIES_MIN_PAGE, ENTRIES_MAX_PAGE, R.string.entries_limit).show();
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
@Override
public void onNumberSet(int number) {
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(ENTRIES_PAGE_LIMIT, number);
editor.apply();
setEntriesPageLimitSummary();
}
}