How to display my couchabase JSON to zk TreeModel? - java

Previously i wrote a query which is queries the proper couchbase file.I have a entity for it:
class SubMenu{
String name,url;
List<SubMenu>; //and of course getters-setters...
}
when i queried i get a arrayList of SubMenu:
Submenu = [name="menu",url="menu",SubMenu[name = "Submenu1",url="submenu1"]
So a SubMenu can contains more Submenu
My JSON:
{
"name":"menu",
"url:"menu",
"subMenu":[{
"name":"submenu",
"url":"submenu",
"wubMenu[]
},
"name":"submenu2",
"url":"submenu2",
"wubMenu[]
]
}
And i want to build a tree like menu which has two children submenu and submenu2...and so on...and if submenu has other submenus go show it.
I started from this example....but i dont know how to solve the override methods...getChild,childCount,isLeaf in my case: ArrayList
Could anyone helps to me? Thanks a lot!

Actually it's pretty straight forward.
IsLeaf actually means is this bean has children or not.
In your example => return true if List<SubMenu> is null or is the size is 0.
childcount => return the size of List<SubMenu>
getChild => return the item at index x of the List. remember also to throw an error or null when index is higher then size of the List.

Related

Retrieve OBJECT from a navigation View (drawer) dynamically android/java

Once again stuck dead in my coding tracks, I come seeking knowledge from the omniscient StackOverflow Community...
My problem is fairly simple. I have populated a Navigation View drawer with objects. These objects toString() methods have been overwritten to display each objects own name in the list. Clicking each object in the drawer list brings up an alert message that should say ("you clicked "+ myDrawerObject.getName()) The problem is in the onNavigationItemSelected listener.
This method returns a MenuItem item which is a view. I am having trouble understanding why i can not treat this view like the object it is supposed to contain. Help is greatly appreciated and thanks in advance!
I understand that the item parameter is returning which menu item has been clicked but if the menu item is an object why cant I call those objects methods?
I have tried grabbing the item return value and calling its getName() method, to no avail. item.getName(); (I understand that item is a view so it doesnt have a "getName()" method
I have tried casting the item value back into an object to no avail
MyObject myObject = (MyObject)item;
//Item is still a view and a view cant be cast into an object it seems
I have even tried putting all created objects of the MyObject type into a static arrayList then trying to match up id's also to no avail.
//in the onNavigationItemSelected listener
int id = item.getItemId()
//somewhere else in the code
for (anotherMyObject : listOfEveryMyObjectEverMade){
anotherMyObject.get(id)
}
/*returns the id of the item in the listview. as i understand a specific id
is assigned to every item in the list. so if an object has never been in that
particular list, it wont have that same id
*/
I must add the the contents of the drawer (the string list) was created dynamically so there is no xml file for the menu or menItems!
I ran into a very similar situation and literally blockaded me for days!
I'm almost 90% positive that there has to be a way out there...
but I personally didn't find it, so instead I went with a work around:
private void setMenus() {
int temp = 0;
//Mine was inside of a fragment, hints the getActivity() call
NavigationView navView22 = getActivity().findViewById(R.id.navigation_view);
MenuItem oneItem = navView22.getMenu().findItem(0);
if (oneItem == null){
Menu m = navView22.getMenu();
SubMenu sm0 = m.addSubMenu(
getResources().getString(R.string.clickToSelectRookies));
sm0.add(0,
temp, temp, getResources().getString(R.string.signAllString));
tempRooks.add(round1.get(0));
/*I had to create the entire menu dynamically, so I did so by creating
subMenus in my menus in numerical order
I then created an arraylist to hold all my objects in that same order
though in retrospect I should have just used a HashMap!!
finally in the onClick I was able to listen for which subMenu item was clicked
and since the menu's and my arraylist of objects corresponded I then pulled
the correct object from my arraylist. I realize this is a little hacky, but
hopefully if you do not find the answer you can do this temporarily.
If you do find a better solution please post so we all can learn. Thank YOU!!
*/
temp++;
SubMenu sm1 = m.addSubMenu(
getResources().getString(R.string.firstRoundPicks));
for (int x = 0; x < round1.size(); x++) {
sm1.add(0, temp, temp, round1.get(x).toString());
tempRooks.add(round1.get(x));
temp++;
}

My loop only prints out one object to my tableview. I´m using Java

private void displayGroupsInRanking() {
for (int i = 0; i < 4; i++)
{
RankingANames.setItems(FXCollections.observableArrayList(groupModel.getListA().get(i).getName()));
System.out.println(RankingANames);
}
RankingBNames.setItems(FXCollections.observableArrayList(groupModel.getListB()));
RankingCNames.setItems(FXCollections.observableArrayList(groupModel.getListC()));
RankingDNames.setItems(FXCollections.observableArrayList(groupModel.getListD()));
}
I´m trying to, to get a specific attribute from an arraylist into a new arraylist. This works fine, but the listview only shows one object?
[The output1
the Code
I'll not rewrite code from your screen to show you the right way to do this but I can tell you what is wrong here.
On every iteration you are creating new collection with exactly one item and then you are using it as items list for table.
That's clearly wrong.
To solve it, you have to first prepare full list of items and then pass it to setItems method.

How to query search through a tree in java

I am trying to return a list of Strings of the nodes whose data is "directory" by returning its true name as children.get(0).children.get(0).data
So far, when I go through the tree, it can find the nodes and add the data that Im looking for, but as I add those strings in the List and them System.out.println the list out, the list changes. It can reset, remove Strings, etc.
Right before the return statement, I print off the list. It is not what gets returned.
!!!beforeoutput[Pictures, Pictures2.0, Pictures, Pictures2.0] //print of the list
!!!beforeoutput[Movies and Pictures, Movies, Pictures] //print of the list
getdirectories [Movies and Pictures, Movies, Pictures] //actual output
Someone had told me to define the list in the global scope, and I did, and literally nothing happened.
I guess what I'm asking for is how does one properly query through a tree. I know its recursive, but when adding the values in the list, it messes up the list.
My method's code + the console + an example tree is posted here: http://pastebin.com/9nXvcCNS
I don't understand what you are trying to actually do here, as your code is messy and fussy. You need to understand that when you return a value from a recursive function call, it will return it to a point from where it has previously been called.
If you have a structure like this,
a
| - b
| - c
where each item is represented as
class Node{
String data;
Node nextNode;
}
and would like your list to contain Strings "a", "b", and "c", you wold have to write something like this:
List<String> allDirectories = new ArrayList<String>();
getDirectories(allDirectories);
private void getDirectories(List<String> allDirectories) {
//somehow get next node
Node node = getNextNode();
//if next node is null, or terminating node: return
if (node == null) return;
allDirectories.add(node.getData());
getDirectories(allDirectories);
}
In other words, try passing your list as a parameter to a method, and don't reset it at the beginning of a function call.

Hierarchical Vaadin Tree from XML(MSDL)

im tryind to build an Vaadin tree from an XML file(MSDL), im stuck at adding child items to my tree. So far i can read from my XML file and display the the tags/info i want but i cant make an Hierarchical strukture out of it , e.g :
i have an XML file with some information about Planets and their moons and the galaxy they are in :
Milky Way
-Sunsystem
-Earth
-"Moon"
-Mars
-Phobos
-Deimos
-Saturn
-Titan
-Tethys
Pinwheel Galaxy
-somesystem
-weirdPlanet1
-moon1
-moon2
-weirdPlanet2
-moon1
-moon2
now i want to have the same strukture in my vaadin tree. i have tryed lots of things but the result was always the same : some null values where added to the tree of i could see only the galaxys but i couldnt expand them or i could see a tree with all the infos but there whee no strukture at all all planets / moons where just listed :/
I'm pretty sure this doesn't have anything to do with the Tree itself. Instead of adding the data directly to the Tree, you can try this:
Parse the XML data into a HierarchicalContainer
Iterate through the HierarchicalContainer with the sample code below and verify that it's identical to your XML file structure
Bind the data container to the tree by calling Tree.setContainerDataSource(Container)
Sample code to iterate through a HierarchicalContainer:
void iterateContainer() {
for(Object rootItemId : myContainer.rootItemIds()) {
Item rootItem = myContainer.getItem(rootItemId);
System.out.println(rootItem.getItemProperty(myLabelProperty).getValue());
iterateChildren(rootItemId, 1);
}
}
void iterateChildren(Object parentItemId, int indent) {
for(Object childItemId : myContainer.getChildren(parentItemId)) {
Item childItem = myContainer.getItem(childItemId);
for(int i = 0; i < indent; i++) {
System.out.print(" ");
}
System.out.println(childItem.getItemProperty(myLabelProperty).getValue());
if(myContainer.hasChildren(childItemId)) {
iterateChildren(childItemId, indent+1);
}
}
}
This is just some untested QnD code, but this should help you to iterate through the container.
edit: Just noticed that my answer could have been (partially) a stupid solution, since Tree already utilizes HierarchicalContainer. You can initialize myContainer HierarchicalContainer myContainer = (HierarchicalContainer) myTree.getContainerDataSource(); and use the code above.
edit2: And if the structure isn't identical, see where it goes wrong and let the debugger do the rest .. :)

Set selected item in ComboBox by Code

I want to selected the first item of a comboBox by code if the value list have only one item. I've tried this:
Comboitem item = new Comboitem();
for(Empresa e : empresasList){
item.setValue(e.getEmpId());
item.setLabel(e.getEmpNombre());
item.setParent(cb_empresa);
}
if(empresasList.size()==1){
idEmpresa = empresasList.get(0).getEmpId();
//cb_empresa.setSelectedIndex(0);
cb_empresa.setSelectedItem(item);
}
But it does not work. I also tried the commented line:
//cb_empresa.setSelectedIndex(0);
Any help?
Thanks!
I don't know whether the item.setParent(cb_empresa); has an immediately effect to the view.
You can try setting the index later, or after refresh the cb_empresa.
First of all, your code is buggy. you have to create as many Comboitems as empresasList.size(). I guess something like
for(Empresa e : empresasList){
Comboitem item = new Comboitem();
item.setValue(e.getEmpId());
item.setLabel(e.getEmpNombre());
cb_empresa.appendItem(item);
}
if(cb_empresa.getItemCount()==1){
cb_empresa.setSelectedIndex(0);
}
Should work. However, there was a bug in ZK that the setSelectedIndex() had to be postponed, as Aloong mentioned. As far as I remember, this bug has been fixed. If not, you can use Event.echoEvent() as a workaround.
for(Empresa e : empresasList){
Comboitem item = new Comboitem();
item.setValue(e.getEmpId());
item.setLabel(e.getEmpNombre());
cb_empresa.appendChild(item);
}
if(cb_empresa.getItemCount()==1){
cb_empresa.setSelectedIndex(0);
}

Categories

Resources