Create MultiSelectedListPreference using ArrayList - java

I'm trying to create a PreferenceActicity.
I need that on of my preferences will be of type MultiSelectedListPreference.
I found this code on the internet:
<MultiSelectListPreference
android:dialogTitle="#string/mode_repeat"
android:key="mode_repeat"
android:summary=""
android:title="#string/mode_repeat"
android:entries="#array/weekdays"
android:entryValues="#array/weekdays_values"
android:defaultValue="#array/empty_array"
/>
The problem is I'm getting the entries and entryValues in runtime.
I'm building the ArrayList while my app is running, the question is how can I set my ArrayList as the entries and as the entryValues?
Do I need to create an empty xml file, which I will re-write during the building of my list?

You wouldn't be able to change the xml in runtime. The solution for your problem is to use the methods setEntries()and setEntryValues()from the MultiSelectListPreference class.
Here's a basic code snippet:
MultiSelectListPreference repeatModePreference = (MultiSelectListPreference) findPreference(Constants. mode_repeat);
repeatModePreference.setEntries(yourEntries); // This is human-readable strings
repeatModePreference.setEntryValues(yourEntryvalues) // The value corresponding to the human-readable string
Hope this helps.

Related

YamlBeans: Turning an object into a hashmap

I have a Yaml file that's something like below:
rules:
- p_table:
["p_event/Name",
"p_fault/Name"]
- s_table:
["s_event/Name",
"s_fault/Name"]
- r_table:
["r_event/Name",
"r_fault/Name"]
So, I can already take the .yml file above and parse through it with YamlBeans and print it out with code like below:
System.out.println(map.get("rules"));
This gives this kind of result:
[{p_table=[p_event/Name, p_fault/Name]},
{s_table=[s_event/Name, s_fault/Name]},
{r_table=[r_event/Name, r_fault/Name]}]
What I would like to do is more on this sort of level, where I can store it in a HashMap and actually use the specifics within the map, with something like this:
HashMap<String, ArrayList<Strings>> Policies = (HashMap)(map.get("rules"));
But when I do that I either have an exception thrown or it just returns null, is there a solution for this should I not be using HashMaps... or can I just not translate objects in such a way? I plan on replacing the String with another type from a different library that uses Strings but wanted to start at the bottom and then go up from there.
The obvious solution would be to remove the sequence from the YAML file:
rules:
p_table:
["p_event/Name",
"p_fault/Name"]
s_table:
["s_event/Name",
"s_fault/Name"]
r_table:
["r_event/Name",
"r_fault/Name"]
If you can't change the YAML file, you need to transform the data after loading it.

how to list all the indices' name of elasticsearch using java?

In my elasticsearch I want to get all the indices' name of the cluster. How can I do using java?
I search the internet but there's no much useful information.
You can definitely do it with the following simple Java code:
List<IndexMetaData> indices = client.admin().cluster()
.prepareState().get().getState()
.getMetaData().getIndices();
The list you obtain contains the details on all the indices available in your ES cluster.
You can use:
client.admin().indices().prepareGetIndex().setFeatures().get().getIndices();
Use setFeatures() without parameter to just get index name. Otherwise, other data, such as MAPPINGS and SETTINGS of index, will also be returned by default.
Thanks for #Val's answer. According to your method, I use it in my projects, the code is:
ClusterStateResponse response = transportClient.admin().cluster() .prepareState()
.execute().actionGet();
String[] indices=response.getState().getMetaData().getConcreteAllIndices();
This method can put all the indices name into a String array. The method works.
there's another method I think but not tried:
ImmutableOpenMap<String, MappingMetaData> mappings = node.client().admin().cluster()
.prepareState().execute().actionGet().getState().‌getMetaData().getIndices().
then, we can get the keys of mappings to get all the indices.
Thanks again!

How to iterate and load property file values in UI using ZK?

I have a property file named "ABC.properties" having values such as
A="APPLE"
B="BALL"
C="CAT"
Now i need to get these values and load in my UI. Have iterate and then need to load because like wise there may be more than 50 values in that property file.
Can anyone please help me with this?
In your zk.xml file, give the path of properties file you wanted to read.
<zk>
<system-config>
<label-location>/path/to/ABC.properties</label-location>
</system-config>
</zk>
If you want to read them in zul file, for example you want to show label having value as A then
< label value="${labels.A}" />
If you want to read properties file's value in listener class then do
Labels.getLabel("A");
First of all, I don't know if there are more items in the property file then you need to fetch.
Note : Multiple properties files all readed in this way and are accessible in the same map.
If yes :
Convert the key's with subsections :
FRUIT.APPLE = apple
FRUIT.ORANGE = orange
TOYS.BALL = ball
TOYS.DOLL = doll
and so on.
The next thing is as #BhushanPatil already mentioned :
In your zk.xml file, give the path of properties file you wanted to read.
<zk>
<system-config>
<label-location>/path/to/ABC.properties</label-location>
</system-config>
</zk>
Then you have the following method getSegmentedLabels under Labels.
This is the method we will use.
So if you want to declare it only in the zul, the next step is what you have to do :
<?xel-method
prefix="prop" name="all" class="org.zkoss.util.resource.Labels"
signature="java.util.Map getSegmentedLabels()"?>
<vlayout forEach="${prop:all().get('FRUIT')}">
<label value="${each.key} : ${each.value}"/>
</vlayout>
The Xel-method is for declaring the static method (only way to acces a static method before ZK 8).
Now we want to get all our FRUIT labels, so we call the getSegmentedLabels with the Xel-method name all().
This return a Map<String,Object> where all the labels are in.
We are interested only in the subsection FRUIT so we get the value behind the key FRUIT.
This object is again a Map<String,Object> wich contains every key under the FRUIT.
In this example it contains <'APPLE','apple'>,<'ORANGE','orange'>.
The next thing we need to do is iterating over the Map and showing all the values of the Map.
This we do with the forEach attribute.
So the each object's value is now an Object of class Entry<String,Object>.
The only thing to show it now is showing the value, with is Entry.getValue().

DOM Parser Example for Objects within Objects

So say I have an XML file that looks like this:
<Object1s>
<Object1>
<Field1></Field1>
<Object2s>
<Object2>
<Field1a></Field1a>
<Field1b></Field1b>
</Object2>
<Object2>
<Field1a></Field1a>
<Field1b></Field1b>
</Object2>
</Object2s>
</Object1>
<Object1>
<Field1></Field1>
<Object2s>
<Object2>
<Field1a></Field1a>
<Field1b></Field1b>
</Object2>
</Object2s>
</Object1>
</Object1s>
The DOM tutorials I've found have not worked when I try and do the same sort of thing. For instance, I want to be able to separate the Object2s by the Object1 that they are in. When following the example given by DOM tutorials where this type of thing doesn't exist in their XML files, I get all the Object2s that are in any Object1 when I try to find them.
Can someone show me an example that handles something like this?
Okay, figured it out. What I do is use the element I declare for each element, and within that call .getElementsBytagName() to get the elements within that element.

Can't get localized strings from xml files correctly

I've been racking my head with this...
I've got a localized strings.xml file in a values-en folder with this example string:
#string/my_string
The string has the following text stored in English: "My String"
When accessing the localized string via a layout, it works fine.
When I try to change it in code, that's where I get problems.
I store the string into an array of strings for later use. The 'context' is passed from my activity to a data class and used with this line of code:
dataStrings = new String[] { (String) context.getResources().getString(R.string.my_string) };
Later, I try to display this string, like so:
buttons[0].setText(dataStrings[0]);
It displays:
#string/my_string
How do I get it to display the string without '#string/', the proper localized string?
You can run getString() directly on the Context object; you don't need to run getResources(). However, this should do the same thing as you're currently doing so I don't think that's the source of your problem.
The first thing to confirm is that what you think is happening is happening. Either use the debugger to check that buttons[0] contains "#string/my_string" or try calling setText() with a hard-coded value to make sure the text is actually being updated on the correct button - e.g. buttons[0].setText("StackOverflow!");

Categories

Resources