How to Get Selected Items from Multi Select List View

How to get Selected items from Multi Select List View

SparseBooleanArray.get returns a boolean, but I believe you need to check it for each position in your list, e.g.

int len = listView.getCount();
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < len; i++)
if (checked.get(i)) {
String item = cont_list.get(i);
/* do whatever you want with the checked item */
}

How to do a multi select list-view and get the selected item?

The First thing you need to do is make a generic something as below:

public class SelectableData<T> 
{
public T Data { get; set; }
public bool Selected { get; set; }
}

Next, you will want to create the ListView, that shows the data and the Switch. In this example, my Data, just has Name and Description inside it. The wrapper, then contains the Selected Boolean.

<ListView ItemsSource="{Binding DataList}" Margin="20">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="0,0,0,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackLayout VerticalOptions="CenterAndExpand">
<Label Text="{Binding Data.Name}" />
<Label Text="{Binding Data.Description}" FontSize="10" />
</StackLayout>
<Switch IsToggled="{Binding Selected}" Grid.Column="1" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

Then bind the ListView with something like this:

public ObservableCollection<SelectableData<ExampleData>> DataList { get; set; }

Where ExampleData is something like:

 public class ExampleData
{
public string Name { get; set; }
public string Description { get; set; }
}

To get the selected item iterate through the DataList using a for each loop and check the Selected Property something like this:

foreach(var item in DataList)
{
item.Selected // true if selected else false.
}

Retrieving the selected items from a multi-select ListView

Use the method getCheckedItemPosition().

Getting selected members from multiselect list view ctrl

Use GetFirstSelectedItemPosition() to find first selected item, then GetNextSelectedItem() for the rest and you're done. :)

C# ListView ItemSelectionChanged Event Multi Select get ONLY last item selected

Have you considered performing the action on a button press instead? That way they can also use Ctrl-Click to select any individual items they want?

Otherwise what you would have to do is wait a certain amount of time before firing the action, known as debouncing, you can read a more about debouncing here: https://stackoverflow.com/a/4517995/984780

I created a class you can use for debouncing:

public class Debounce {
private Action _action;
private bool _isThreadRunning;
private Thread _thread;
private DateTime _runAt;
private double _waitSeconds;

private Debounce(double waitSeconds, Action action) {
_action = action;
_waitSeconds = waitSeconds;
}

private void Invoke() {
_runAt = DateTime.Now.AddSeconds(_waitSeconds);

lock(this) {
if(!_isThreadRunning) {
_isThreadRunning = true;

_thread = new Thread(() => {
while(true) {
Thread.Sleep(100);

lock(this) {
if(DateTime.Now > _runAt) {
_action();
_isThreadRunning = false;
_thread = null;
break;
}
}
}
});

_thread.Start();
}
}
}

private static Dictionary<Action, Debounce> __debounces;
private static Dictionary<Action, Debounce> _debounces {
get {
if(__debounces == null) {
__debounces = new Dictionary<Action, Debounce>();
}

return __debounces;
}
}

public static void Run(double waitSeconds, Action action) {
Debounce debounce;

if(!_debounces.TryGetValue(action, out debounce)) {
debounce = new Debounce(waitSeconds, action);
_debounces.Add(action, debounce);
}

debounce._waitSeconds = waitSeconds;
debounce.Invoke();
}
}

Then you can change your code to this:

private void lvTitles_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
Debounce.Run(5, () => MessageBox.Show(e.Item.Text.ToString()));
}

This should work no matter how they select items, it will run your code 5 seconds after their last selection action.

I just wrote this class and did a quick test, a more thorough test would be advised. In any case hopefully it's enough to get the idea.

How do I post back all selected items in a multi-select list so it is included in my view model?

The posted value of a select, will always be primitive types (string, int, etc.). Accordingly, the posted value of a select multiple will simply be an enumerable of primitive types. As such, you cannot bind directly to something like List<UserSiteItem>. Instead, you need to bind to a property of type List<string> or List<int> (depending on whether you've customized identity's user PK), since in this case, it will be a list of user ids being posted back.



Related Topics



Leave a reply



Submit