Unable to show the selected item in the Wp7 Listpicker control - windows-phone-7

Basically i am trying to pull the contacts from the phone and showing them in the Listpicker control for a feature in my app. I have two Listpickers, one for name of contacts list and the other showing the list of phonenumbers for the chosen contact.
Here is my code:
//Declarations
ContactsSearchEventArgs e1;
String SelectedName;
String SelectedNumber;
List<string> contacts = new List<string>();
List<string> phnum = new List<string>();
public AddressBook() // Constructor
{
InitializeComponent();
Contacts contacts = new Contacts();
contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
contacts.SearchAsync(string.Empty,FilterKind.None,null);
}
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
e1 = e;
foreach (var result in e.Results)
{
if (result.PhoneNumbers.Count() != 0)
{
contacts.Add(result.DisplayName.ToString());
}
}
Namelist.ItemsSource = contacts;
}
private void Namelist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedName = (sender as ListPicker).SelectedItem.ToString();
phnum.Clear();
foreach (var result in e1.Results)
{
if (SelectedName == result.DisplayName)
{
phnum.Add(result.PhoneNumbers.FirstOrDefault().ToString());
}
}
Numbers.ItemsSource = phnum;
}
private void Numbers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedNumber = (sender as ListPicker).SelectedItem.ToString();
}
Am able to populate the Numberlist with phonenumbers for the chosen name at the Listpicker background, but the number is not showing up in the front. I think Numbers_SelectionChanged() event is called only one time when the page loads and am not seeing it triggerd when i change the contact list. Anyone has an idea of where am going wrong ?

If you change
List<string>
To
ObservableCollection<string>
this should work.
Also then you only need to set the ItemSource once, in Xaml or you constructor.
But you may run into another issue with the November 2011 Toolkit and ListPicker.
See more in thread.

private void Namelist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedName = (sender as ListPicker).SelectedItem.ToString();
phnum = new List<string>(); // Changed instead of phnum.Clear()
foreach (var result in e1.Results)
{
if (SelectedName == result.DisplayName)
{
phnum.Add(result.PhoneNumbers.FirstOrDefault().ToString());
}
}
Numbers.ItemsSource = phnum;
}
This works !!. While debugging i found its phnum.Clear() giving a problem. So i thought to create a new instance of phnum list for selected contact.

Related

xamarin forms syncfusion ListView ItemAppearing

I use syncfusion listview to create listview on xamarin forms
I want to use the ItemAppearing option in listview
I used this EXAMPLE on website:https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.SfListView.html
and this EXAMPLE: https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.SfListView~ItemAppearing_EV.html#ExampleBookmark
I use this example and found this problem
ListView.ItemAppearing +=listView_ItemAppearing;
public void listView_ItemAppearing(object sender, Syncfusion.ListView.XForms.ItemAppearingEventArgs e)
{
var temp= e.ItemData as IEnumerable<ListViewCall>;
//temp.ToList();
}
I cast e.ItemData to List<ListViewCall> and get null
e.ItemData has data but var temp is null
Why would this be?
ItemData :Gets the underlying data object of the ListViewItem when item appearing from the bound data source.
so e.ItemData will return you the binding object. like the example above,it will return the object BookInfo.
public void listView_ItemAppearing(object sender, Syncfusion.ListView.XForms.ItemAppearingEventArgs e)
{
var temp= e.ItemData as BookInfo;
}
public void listView_ItemAppearing(object sender, Syncfusion.ListView.XForms.ItemAppearingEventArgs e)
{
if (e.ItemData is GroupResult)
{
var listViewCalls = (e.ItemData as GroupResult).Items as EnumerableQuery<ListViewCall>;
foreach (var listViewCall in listViewCalls)
{
}
}
else if (e.ItemData is ListViewCall)
{
var listViewCall = e.ItemData as ListViewCall;
}
// foreach (Object obj in e.ItemData.GetType().GetProperties(System.Reflection.BindingFlags.Public | BindingFlags.Instance))
// {
// string s = (obj as Call).Title;
//}
}

How to get list item index when user clicks on the button which is in list item in windows phone 8

I've list box in my application.
Below is the screen shot.
When user clicks on the list item, then i'm displaying detailed page.
It is handling in below selection changed listener.
private void companiesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//get the selected item from list
Company selectedItem = (Company)e.AddedItems[0];
Uri uri = new Uri("/CompanyDetailsPage.xaml", UriKind.Relative);
//navigate to target page
this.NavigationService.Navigate(uri);
FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
root.DataContext = selectedItem;
}
}
Upto this it is fine.
Now when the user clicks on the Delete button which is on the item,
then i've to delete that item from the list.
private void Del_Btn_clicked(object sender, RoutedEventArgs e)
{
//get the Corresponding item from list i.e. On which delete button is placed.
//Delete saved company from the database
}
I'm unable to get that particular list item index on which the delete button is placed.
Ho could I get.
Thanks.
You can retrieve the button by casting the sender parameter. From there, you can retrieve the company by casting the DataContext property:
private void Del_Btn_clicked(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
var company = (Company)button.DataContext;
// ...
}
do get the index of the listbox you can direct set the property
SelectedIndex = {Binding asd,Mode=TwoWay}
then in viewmodel
make a property
private int _asd;
public int asd
{
get
{
return _asd;
}
set
{
_asd= value;
}
}
by this you will get the index of selected item ...
hopes it might help ..

How to delete the listbox item in wp7?

Listbox having 2 buttons.When click on button need to delete the item from that listbox.
please tell me how to acheive that?
List<SampleCheckedData> interestrates = new List<SampleCheckedData>();
interestrates = (from rts in xmlDocu.Descendants("Friend")
select new SampleCheckedData
{
Id = (string)rts.Element("userid"),
Name = (string)rts.Element("name"),
Icon = (string)rts.Element("imageurl"),
VisibleStatus = (string)rts.Element("visiblestatus"),
AppStatus = (string)rts.Element("loginstatus"),
imgBubble =bitmapRed,
}).ToList<SampleCheckedData>();
this.lstImages.ItemsSource = interestrates;
private void btnAccept_MouseEnter(object sender, MouseEventArgs e)
{
int _id = int.Parse(((System.Windows.FrameworkElement)(e.OriginalSource)).Tag.ToString());
lstFriendRequuest.Items.RemoveAt(lstFriendRequuest.SelectedIndex);
}
To delete the selected item,
listbox.Items.RemoveAt(listbox.SelectedIndex);
Make your collection available globally on this page, and now you can manipulate on it easily from btnAccept_MouseEnter event:
public interestrates;
...
{
interestrates = ...
this.lstImages.ItemsSource = interestrates;
}
private void btnAccept_MouseEnter(object sender, MouseEventArgs e)
{
interestrates.RemoveAt(lstFriendRequuest.SelectedIndex);
}
Also, make sure that a click on a ListBox item changes SelectedIndex accordingly

Cant get selected listboxitem to show on my detail page

I have made and rss reader and have a listbox with all the feeds in it. When I select one item and want to see the whole article I only get the selectedindex number, 0,1,2 and so on.
Here is my code:
private void feedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
NavigationService.Navigate(new Uri("/DetailsPage.xaml?feeditem=" + listBox.SelectedIndex, UriKind.Relative));
}
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string feeditem = "";
if (NavigationContext.QueryString.TryGetValue("feeditem", out feeditem))
{
this.textBlock1.Text = feeditem;
}
}
What am I missing here?

How to get all contacts and write into file for Windows phone 7

The problem I want to solve:
Get all the contacts info like name and Mobile phone and write it into file and save in ISO.
How to use SearchAsync if I want to search available contacts in the phone?
How to iterate the return-results and write to file one by one of the contact into a file?
Here's the code I have:
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
Contacts contacts = new Contacts();
contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
contacts.SearchAsync(displayName,FilterKind.DisplayName,null);
//search for all contacts
contacts.SearchAsync(string.Empty, FilterKind.None, null);
}
Update:
The below code throw NullException error if the PhoneNumber is Empty. Why?
How to get all the possibile phone number other than result.PhoneNumbers.FirstOrDefault().ToString();
Same question for EmailAddresses
Using this to search all contacts in the phone:
contacts.SearchAsync(searchterm, FilterKind.None, null);
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
int intTTL = e.Results.Count();
if (intTTL != 0)
{
MessageBox.Show(intTTL.ToString());
foreach (var result in e.Results)
{
string strTTL;
string strName = result.DisplayName;
string MobileNo = result.PhoneNumbers.FirstOrDefault().ToString();
strTTL = strName + "," + MobileNo;
MessageBox.Show(strTTL);
}
else
{
MessageBox.Show("You have not entered any contact info at all.");
}
}
Have a look at this article to see how to get the Contacts:
To iterate:
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
foreach(var contact in e.Results)
{
// write to Isolated storage
}
}
See this MSDN page for writing files in isolated storage

Resources