Checkbox preference using .java - java

I am currently working on an android project where I need to create a SettingsActivity where I will have a variable number of checkboxes depending on an int from an SQLite database. I figured that the best way to do this was to create a for loop inside the SettingsActivity .java code, which would create a new checkbox preference for each time it runs the loop(as of now I only have a theoretical idea of how it would work). My problem would be to create the checkbox preference in .java rather than the XML. I searched the internet and could only find one example that had about 300 lines of code in it, and I can't believe that it needs to be as complicated as it was in that example. Does anyone have a helpful link, an idea how to do this or even knows a better way to make a variable number of checkboxes?
Sorry that I don't have any relevant code to post, but here is the link to the example I found http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/preference/CheckBoxPreference.java

http://developer.android.com/reference/android/preference/CheckBoxPreference.html
It also inherits a lot of methods from Preference: http://developer.android.com/reference/android/preference/Preference.html.
You can pass the constructor an attribute set where you specify summaryOn and summaryOff and the title
The link you posted is the source code of android. You don't need to implement that in your application as it's already in the android environment

Related

JFace - How to remove preference property?

I'm wokring with a little JFace preference store where I can add preferences straight forward enough, but have trouble when I want to remove one of the preferences I've put in.
I can't see anything in the APIs that allows for removal. What is the correct way for doing this?
Assuming you are using IPreferenceStore you call setToDefault("pref id") to reset a preference to its default value.
This is weird !!
I asked this same question 3 days ago in the eclipse JFace forum: https://www.eclipse.org/forums/index.php/t/1088245
I only got answers not related to my need.
The answer is that it is not possible. Also you can not set the value or the default of a preference to "null"
In my app (JMSToolBox), the need is to "cleanup" the file that is backing the PersistenceStore as the user may store a lot of "keys/preferences" that may become obsolete at some point in time. I wanted a way to "remove/delete" them from the file to keep the PreferenceStore file as compact as possible.
I ended up writing my own version of PreferenceStore that exposes the "remove" method from the internal "Properties" object used by PreferenceStore. This class is not designed to fullfill my need.
Code is here

android: Changing values of constants/variables depending on the application using the activity

I am working on an AlertDialog Fragment which is supposed to pop up under certain circumstances, for example usage days of the app. I defined a constant for the current App and simply check if requirements are met before the onCreate is called like this:
if(statistics.getDays()>REQUIREMENT_VARIABLE)
{
onCreateTest=true;
}
Now I was planning to use this Activity with other applications/build variants (and change the values of the check variables for each individual app) however I was not sure how to go about checking which application is using it. Is a config xml where I define the packages in strings a good way to approach this? Thanks in advance.
I think you should define these variable in your app build.gradle specially for the build variant.
This post may help you.
Hope this helps.
Sorry for my english.
Create new java class along with your activity. Access and use that java class from another application to initialize check variables depends upon your business logic. And use those variables in the mentioned condition.

How to compare two identical methods in eclipse?

I'm doing android app from a book, for some reason the exact method source code I wrote myself does not work as expected and I am trying to debug it.
I have two exact chunks of code, my method and the sample method.
How to compare them in eclipse?
Select both files by clicking the first, then while holding CTRL click on the second.
Now both of them got selected.
Now click one of them (doesn't matter which one) with the right mouse button.
From the appearing context menu choose:
Compare
Each other
Now you can do a text compare.
Did I get right that neither of the sample method nor your method do what they should?
Then there are two possibilities why the code won't do what it should:
The book is obsolet
You made something wrong
either way, google for your specific problem, maybe someone else has encountered it as well and already solved it.
for your Question: already answered in another comment
use beyond compare, it's a great tool for comparing classes, and methods! download beyond compare

Storing/Retrieving Data with Shared Preferences

I have an application with 7 separate views. In each of those views there is an option to answer a 'yes' or 'no.' We are trying to save these 'yes' or 'no' values using shared preferences. Then, we want to have a new view/layout and be able to call those values from shared preferences. How do I go about doing that? My group and I have tried several different ways but cannot seem to get it to work. I know I don't have any code posted but it's because my code is just in bits and pieces. Thanks.
In your case I would not use getSharedPreferences() because it creates separate prefs files. Instead, you should use the main preferences file (call getPreferences() from the main Activity). This will be easier for you to manage than using shared preferences files.
I would write some code to show you how to do this, but the Android training page on this is about as clear as can be. Can you look it over and see if it answers your question? If not, post back here for clarification and I'll do what I can to help.
Here's the link to the page: https://developer.android.com/training/basics/data-storage/shared-preferences.html
Simple Do following Steps :
1)call getDefaultSharedPreferences() to create shared preferences
2) for each view just put values
3)in final view just create preference using getDefaultSharedPreferences() and then retrieve all the values
Hope this will help you

Refactor hardcoded variable across multiple classes into one global variable?

(First time please be gentle etc. etc.)
Let's say I was lazy/unthinking/pressed for time and hardcoded a string instead of making a global variable. And I repeated this mistake over hundreds of classes and test cases that I wrote. Now, I want to fix this, since I found out I'll eventually need to update that string. Is there some refactoring method in Eclipse or elsewhere that will let me replace all instances of that specific string with a global variable?
I can think of a programmatic solution, to run through all those files and replace the string, but I'd prefer not to go down that route unless absolutely necessary.
Thanks a lot!
Well you can use search and replace within Eclipse across all the files in your project, for one thing. You don't need to write that yourself.
It doesn't look like the "Extract Constant" refactoring of Eclipse is willing to extract it across classes, unfortunately. That would obviously be the nicer solution.
Eclipse provide Refactor option.
right click on value which you want to replace with variable, you will see option Refactor.
this will help you replace value from all other occurance.
You can find referenced place to an element in eclipse as following:
Selected element.(variable of String in your case)
Sight click on selected element
Selected menu References
Select Workspace item for finding all references in your workspace
Then you can edit evry item in result
Exist another way such as using search and replace feature.

Categories