Get Selected item from List View

343 views
Skip to first unread message

Albert

unread,
Aug 14, 2012, 11:44:56 AM8/14/12
to android...@googlegroups.com
Hello ...
i have list view that i bind it with ArrayListObservable of person
like that :
<ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        binding:itemSource="ADAPTER({source=persons, template=@layout/list_item})"/>

and this is list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:binding="http://www.gueei.com/android-binding/"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
    binding:text="name"
/>
and it's work nice

but how can i get the selected person from it when the user click over any person item using mvvm ??
it try to set attribute ( binding:selectedItem="selectedPerson") in the list but it doesn't work ..
i mean for example when the user click at any item of the list it displays selected person name or etc ...

thanks in advance :)

Albert

unread,
Aug 14, 2012, 11:50:13 AM8/14/12
to android...@googlegroups.com
i remember some thing ...
i try to set command in the person model
so every person will have a command that display it's name for example ...
but that's mean if i have 100 person i will have 100 command instance in the device memory .. and all this command do the same work
so i want only one command on the personVM to display any selected person from the list

martin martinez

unread,
Aug 14, 2012, 12:54:17 PM8/14/12
to android...@googlegroups.com
Unfortunately I've asked about this to Andy myself and the answer is now and it has a lot of sense, given the fact that the ViewModel can have any Hierarchy it's impossible to the AB FW to handle it. One way to solve the issue is to have an Attribute , i.e: "Parent", this parent is the one who actually have the Command.

Another way is that you create a Generic Tree structure and use always this structure to bind the view.

Martin

samer shatta

unread,
Aug 14, 2012, 3:25:45 PM8/14/12
to android...@googlegroups.com
can you explain more Mr.Martin ...

Thiago Lacerda Siqueira

unread,
Aug 14, 2012, 4:13:48 PM8/14/12
to android...@googlegroups.com
Hi Albert, 

If i understand well your need, take a look in SingleChoiceList example of the markup demo.

Here is the code:

List:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView
   android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:choiceMode="singleChoice"
    android:layout_weight="1"
    binding:checkedItemPosition="CheckedPosition"
    binding:itemSource="GenreList"
    binding:itemTemplate="@layout/simple_list_item_single_choice"
    />
</LinearLayout>

Item:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    binding:text="."
/>

I hope it helps...

Best Regards.

Thiago Lacerda


2012/8/14 samer shatta <samer....@gmail.com>

samer shatta

unread,
Aug 14, 2012, 4:33:37 PM8/14/12
to android...@googlegroups.com
Thanks Thiago Lacerda
that what i want exactly .
but now if any item of the list clicked the (
CheckedPosition) will be changed that is very nice .
but how can know that list is clicked at any position ??
is there a way to know if the (CheckedPosition) has been changed if there is my problem will solved .

Thiago Lacerda Siqueira

unread,
Aug 14, 2012, 4:39:47 PM8/14/12
to android...@googlegroups.com
You can use binding:onItemClicked event of list view.
Example:

xml

<ListView
   android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    binding:itemSource="MusicList"
    binding:itemTemplate="@layout/music_item"
    binding:onItemClicked="ItemClickedCommand"
        binding:onItemLongClicked="ItemLongClickedCommand"
        binding:longClickedItem="ClickedItem"
    binding:clickedItem="ClickedItem"
    />

and on view model

.
.
.
public final Command ItemClickedCommand = new Command(){
public void Invoke(View view, Object... args) {
Toast.makeText(view.getContext(),((MusicItem)ClickedItem.get()).Title.get(), Toast.LENGTH_SHORT)
.show();
}
};
.
.
.

I hope it helps

Best Regards,
Message has been deleted

b n patil

unread,
Jul 19, 2013, 4:27:54 AM7/19/13
to android...@googlegroups.com
Hi Thiago Lacerda ,


thanks for your example. From your example i am able to get name/label from the selected position. How to get the selected position from the listview itself.

Thanks in advance.

Andy Tsui

unread,
Jul 19, 2013, 9:37:02 PM7/19/13
to android...@googlegroups.com
SelectedItem /SelectedItems only works with ListView which the mode is set to be selectable/multi-selectable:

But if you have defined some other interaction in the item level, that won't work. I would suggest you do the selection in item-level, for example:

class Person{
StringObs Name,
BoolObs Selected
}

person_template.xml:

<linearlayout>
<textview binding:text="Name"/>
<Checkbox binding:checked="Selected"/>
...

In this case, you can select that person by clicking the checkbox (you can also do it in the linearlayout, which makes similar behavior as the listview's selection). Since the selection state lives in the object tree, you can easily read the result. 

Andy

--
You received this message because you are subscribed to the Google Groups "AndroidBinding" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbindin...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages