How to set ListView data binding?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

How to set ListView data binding?

treponema
Hi,
I did not found how to bind a ListView to an ArrayList (although I found samples about TableViews). If I use setListData with an ArrayList, the strings are displayed, but if the class has more properties, can someone explains me how to tell the ListView which one to display, which one is the key?
Thanks...

P.S. : For instance: A list of person objects (properties: id, name, age, ...), I would like the ListView to display the name and return the id of the selected person.
Reply | Threaded
Open this post in threaded view
|

re: How to set ListView data binding?

treponema

I just realized that something has removed the types of the ArrayLists from my message. For the first sentence it was Something, for the second it was String.

> Message du 13/08/12 18:27
> De : "treponema"
> A : [hidden email]
> Copie à :
> Objet : How to set ListView data binding?
>
> Hi,
> I did not found how to bind a ListView to an ArrayList (although I found samples about TableViews). If I use setListData with an ArrayList, the strings are displayed, but if the class has more properties, can someone explains me how to tell the ListView which one to display, which one is the key?
> Thanks...
>
> P.S. : For instance: A list of person objects (properties: id, name, age, ...), I would like the ListView to display the name and return the id of the selected person.
>
Reply | Threaded
Open this post in threaded view
|

Re: How to set ListView data binding?

Sandro Martini
Hi,
this is a useful thing to show, but currently we don't have a small
sample of it ...

Did you tried with manual binding ?
See here: http://pivot.apache.org/tutorials/property-binding.html

and then maybe even:
http://pivot.apache.org/tutorials/data-binding.html
http://pivot.apache.org/tutorials/stock-tracker.data-binding.html

Keep us updated.

Bye,
Sandro
Reply | Threaded
Open this post in threaded view
|

Re: How to set ListView data binding?

treponema
Hi,
Thanks for the links, but I had already read twice all of them.
If I have an ArryList of person objects, is the only way to display their names in a ListView to create an ArrayList of Strings filled with their names and pass it to setListData ? I thought I could set the property to display with setListDataKey, but I do not understand how to do that.
Bye...



> Message du 14/08/12 11:48
> De : "Sandro Martini"
> A : [hidden email]
> Copie à :
> Objet : Re: How to set ListView data binding?
>
> Hi,
> this is a useful thing to show, but currently we don't have a small
> sample of it ...
>
> Did you tried with manual binding ?
> See here: http://pivot.apache.org/tutorials/property-binding.html
>
> and then maybe even:
> http://pivot.apache.org/tutorials/data-binding.html
> http://pivot.apache.org/tutorials/stock-tracker.data-binding.html
>
> Keep us updated.
>
> Bye,
> Sandro
>
Reply | Threaded
Open this post in threaded view
|

Re: How to set ListView data binding?

Sandro Martini
Hi,

> Thanks for the links, but I had already read twice all of them.
Ok, just to be sure :-) ...

> If I have an ArryList of person objects, is the only way to display their names in a ListView to create an ArrayList of Strings filled with their names and pass it to setListData ? I thought I could set the property to display with setListDataKey, but I do not understand how to do that.
I'm sorry but I haven't a sample of this (maybe Roger or others have),
so I have to write a minimal sample for it (could be useful for others
too).
Initial approach (even for me, to remember what to do):
copy some classes from
tutorials/org.apache.pivot.tutorials.databinding.*  into
tests/org.apache.pivot.tests.*  and see what happens trying to set
setListDataKey on a property of bean for the binding, maybe using
BeanAdapter ... all this just as general idea.

Just found something that could give some info on this:
org.apache.pivot.tests.DataBindingTest , under the tests subproject.
In the meantime, try to look at it ...

Let's update.

Bye,
Sandro
Reply | Threaded
Open this post in threaded view
|

Re: How to set ListView data binding?

gk_brown@verizon.net
> If I have an ArryList of person objects, is the only way to display their names in a ListView to create an ArrayList of Strings filled with their names and pass it to setListData ?

You can use a custom list item renderer for this. See this example:

http://svn.apache.org/repos/asf/pivot/trunk/demos/src/org/apache/pivot/demos/rss/

> I thought I could set the property to display with setListDataKey, but I do not understand how to do that.

This method sets the name of the key that is used in context binding (i.e. load() and store()), so this is probably not what you want.

Greg

Reply | Threaded
Open this post in threaded view
|

Re: How to set ListView data binding?

Roger and Beth Whitcomb
What I was thinking is that if your Person object's "toString" method returns whatever you want to display in the ListView.  Then just use List<Person> as the ListView data.  Then the "getSelectedItem" can return your Person object and you can do whatever you want with it.....

HTH,
~Roger

On Aug 14, 2012, at 9:39 AM, Greg Brown wrote:

>> If I have an ArryList of person objects, is the only way to display their names in a ListView to create an ArrayList of Strings filled with their names and pass it to setListData ?
>
> You can use a custom list item renderer for this. See this example:
>
> http://svn.apache.org/repos/asf/pivot/trunk/demos/src/org/apache/pivot/demos/rss/
>
>> I thought I could set the property to display with setListDataKey, but I do not understand how to do that.
>
> This method sets the name of the key that is used in context binding (i.e. load() and store()), so this is probably not what you want.
>
> Greg
>
>

Reply | Threaded
Open this post in threaded view
|

Re: How to set ListView data binding?

treponema
Thank you all,
I have written a custom list item renderer like this one:

listView.setItemRenderer(new ListViewItemRenderer() {
@Override
public void render(Object item, int index, ListView listView, boolean selected,
boolean checked, boolean highlighted, boolean disabled) {
if (item != null) {
item = ((Person) item).getName();
}
super.render(item, index, listView, selected, checked, highlighted, disabled);
}
@Override
public String toString(Object item) {
return ((Person) item).getName();
}
});

It is working fine.
Bye...






> Message du 14/08/12 19:25
> De : "Roger Whitcomb"
> A : [hidden email]
> Copie à :
> Objet : Re: How to set ListView data binding?
>
> What I was thinking is that if your Person object's "toString" method returns whatever you want to display in the ListView. Then just use List as the ListView data. Then the "getSelectedItem" can return your Person object and you can do whatever you want with it.....
>
> HTH,
> ~Roger
>
> On Aug 14, 2012, at 9:39 AM, Greg Brown wrote:
>
> >> If I have an ArryList of person objects, is the only way to display their names in a ListView to create an ArrayList of Strings filled with their names and pass it to setListData ?
> >
> > You can use a custom list item renderer for this. See this example:
> >
> > http://svn.apache.org/repos/asf/pivot/trunk/demos/src/org/apache/pivot/demos/rss/
> >
> >> I thought I could set the property to display with setListDataKey, but I do not understand how to do that.
> >
> > This method sets the name of the key that is used in context binding (i.e. load() and store()), so this is probably not what you want.
> >
> > Greg
> >
> >
>
>
Reply | Threaded
Open this post in threaded view
|

Re: How to set ListView data binding?

xiaoyuandlg
This post has NOT been accepted by the mailing list yet.
In reply to this post by treponema
This is the exact same question that I am eager to ask. A good one that helped a lot on my C# ListView data binding problem. I learnt a lot from this.