I have an Array with the IDs of several checkboxes. In onResume I wish to check whether the booleans of those checkboxes are true or false (using a loop) in order to adapt the view. Here is my Code. It does not work as it tells me that every value is false. Could someone tell me why this is happening?
ArrayList<Integer> boxArray = new ArrayList<Integer>();
boxArray.add(R.id.boxA);
boxArray.add(R.id.boxB);
boxArray.add(R.id.boxC);
And in onReturn:
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(this);
for (int i=0; i<boxArray.size(); i++) {
if (preferences.getBoolean
(findViewById(boxArray.get(i)).toString(), false) == true) {
//change the view of the checkbox to checked if its value is true
CheckBox currentBox = (CheckBox) findViewById(boxArray.get(i));
currentBox.setChecked(true);
}
}
How do you set the value in shared preferences?
When a box is checked do you do something like
preferences.edit().putBoolean(TheCheckedBoxId, true);
If the shared preference getBoolean() method is always defaulting to false then either the value is not there at all or it is just false. I think you can try
preferences.contains(findViewById(boxArray.get(i)).toString())
to see if that key exists at all. There's also the preferences.getAll() method.
Related
I want to change dinamically the list entries and entryvalues of a listpreference. Well, finally it works, the list changes, but the problem is that I can see the new values only when I click the second time or more in the listpreference in the preference screen.
The first time, the list is always the original list, from 1 to 10.
numeroIntentosLP.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
#Override
public boolean onPreferenceClick(Preference preference)
{
CharSequence[] NuevosValores = new String[10 - LineaActual];
int Indice = 0;
for(int i = LineaActual + 1; i <= 10; i++)
{
NuevosValores[Indice++] = String.valueOf(i);
}
numeroIntentosLP.setEntries(NuevosValores);
numeroIntentosLP.setEntryValues(NuevosValores);
return true;
}
});
I have tried with numeroIntentosLP.setOnPreferenceChangeListener, but the same result.
LineaActual is an integer value that I passed from MainActivity to PreferenceActivity. This is useful to me to know the new start value for the list.
So, everytime I open the preference screen and I click for the first time in the listpreference, I always get numbers 1 to 10, but when I click again, no matter how many times, I get what I want, I mean the list from LineaActual to 10.
Thanks in advance.
Ok, I found the solution. It was as easy as put the code directly in the onCreate, not in the listeners.
Thank you samgak, I saw your answer a little bit late and you're right, thank you anyway.
I have created checkbox dynamically. Based on the require list size just am creating new dynamic check box in a repeated manner and am also setting the Id for that. Now i want to do check it in other loop.
for(int i=0;i<require.size();i++)
{
//From Requirements
requirement=require.get(i);
RelativeLayout rl1 = new RelativeLayout(getActivity());
rl1.setBackgroundResource(R.drawable.listviewdesign);
l1.setOrientation(LinearLayout.VERTICAL);
req1 = new CheckBox(getActivity());
rl1.addView(req1);
req1.setId(Integer.parseInt(requirement.r_id));
Log.i("getid",Integer.toString(req1.getId()));
li.add(Integer.toString(req1.getId()));
}
In this loop am just checking element of li and proj_require1 values. If both are equal then I want to make the CheckBox as checked. For that i have written the code here.
for(int i=0;i<li.size();i++)
{
//li.get(i);
req1 = (CheckBox) container.findViewById(i);
String sr = req1.toString();
for(int j=0;j<proj_require1.size();j++)
{
pr = proj_require1.get(j);
if(sr.equals(pr.rid))
{
req1.setChecked(!req1.isChecked());
}
else
{
req1.setChecked(req1.isChecked());
}
}
}
But my doubt is in first loop am creating the CheckBox based on the size of require object. So every time it creates the CheckBox inside the loop. But in second loop am trying to access the checkbox which am created in the first loop. Could anyone please help me to solve this problem? The only way is i can create the CheckBox in the first loop. I want to access it in other loop. Is it possible?
A fairly normal scenario: an Android application has a preferences activity, and selecting an option from a ListPreference triggers code to change that ListPreference's summary text. ie: Selecting "Green" from a color ListPreference would change the ListPreference's summary text to "Green" through a onPreferenceChange callback.
I'd like to be able to use the Android JUnit testing to confirm that these summary changes are all being performed correctly. However, there seems to be very little information out there on how to do this.
I've tried variations of using setValue() on ListPreference, both in the test thread and through runOnUiThread(), with no success - this doesn't trigger the call to onPreferenceChange(). I've also tried getInstrumentation().waitForIdleSync() after calling setValue(), but that's also with no success.
So, my question is: how is this done?
Thanks!
A few more hours of work produced this working solution, but I'm curious if anyone else has a better solution. This code was inspired by this solution to a similar question, but this case is different in two ways:
It's intended for use by Android JUnit, which means it needs to call the ListPreference UI clicks via runOnUiThread().
It expects there to be preference categories in use, which complicate finding the position (relative to the entire preferences list) to click. The above mentioned solution only works in cases without preference categories.
This method will accept the key for a particular ListPreference item, along with the position of the item in the list to be clicked. It will then perform that list item click, and other code would do the checking I'm looking for.
Note that this requires setActivityInitialTouchMode(true); to be set before the getActivity() call in the setUp() method.
private void clickListPreference(String _listPreferenceKey, int _listItemPos){
final String listPreferenceKey = _listPreferenceKey;
final int listItemPos = _listItemPos;
mActivity.runOnUiThread(
new Runnable() {
public void run() {
// get a handle to the particular ListPreference
ListPreference listPreference= (ListPreference) mActivity.findPreference(listPreferenceKey);
// bring up the dialog box
mActivity.getPreferenceScreen().onItemClick( null, null, getPreferencePosition(), 0 );
// click the requested item
AlertDialog listDialog = (AlertDialog) listPreference.getDialog();
ListView listView = listDialog.getListView();
listView.performItemClick(listView, listItemPos, 0);
}
/***
* Finding a ListPreference is difficult when Preference Categories are involved,
* as the category header itself counts as a position in the preferences screen
* list.
*
* This method iterates over the preference items inside preference categories
* to find the ListPreference that is wanted.
*
* #return The position of the ListPreference relative to the entire preferences screen list
*/
private int getPreferencePosition(){
int counter = 0;
PreferenceScreen screen = mActivity.getPreferenceScreen();
// loop over categories
for (int i = 0; i < screen.getPreferenceCount(); i++){
PreferenceCategory cat = (PreferenceCategory) screen.getPreference(i);
counter++;
// loop over category items
for (int j = 0; j < cat.getPreferenceCount(); j++){
if (cat.getPreference(j).getKey().contentEquals(listPreferenceKey)){
return counter;
}
counter++;
}
}
return 0; // did not match
}
}
);
getInstrumentation().waitForIdleSync();
}
I have a PreferenceActivity containing a number of CheckBoxPreference and I want to make sure that at least one of them is selected, any suggestion on how to do it?
Thanks
I ended up registering the same instance of Preference.OnPreferenceChangeListener on all my CheckBoxPreference. The listener keeps a set with my CheckBoxPreference and reacts when the user unchecks one, returning false if it's the only one checked.
Cant you use .setChecked(true) on the checkbox control
i.e.
// get the control
final CheckBox chkRemember = (CheckBox) findViewById(R.id.checkbox);
// pull th evalue from your preferences
strChecked = rwPref.readWriteUserSetting(DevDroidSLX.this, "Read", "CheckboxValueA" , "" );
if ( strChecked.equalsIgnoreCase("True"))
{
chkRemember.setChecked(true);
}
else
{
chkRemember.setChecked(false);
}
I have a ListPreference and want to verify if there is no selection to make some code/treatement. How can i do this ?
I have this to verify the selection:
if (Integer.valueOf(choice) == 0) {
What code to verify if not selection?
Thank you for your help.
If there is a preference selected for this ListPreference, then it will be saved in your SharedPreferences. You can test against this value by doing something like this:
private Boolean prefHasSelection(String prefId){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
String yourPref = sp.getString(prefId, null);
return (yourPref != null);
}
prefHasSelection("yourPrefId"); // returns true if something is set
You could call this method at any point in your application lifecycle to determine if the preference has been set.