locked
Why selectedIndex are always equal zero???? RRS feed

  • Question

  • <ul class='cdo-cloud>

    <!--Todo: add links and weight--> <li>th1</li> <li>th2</li> </ul>

     foreach(var div in doc)
                 {
                     
                     
                     string regrex = div.SelectSingleNode(".//ul[@class='cdo-cloud']").InnerText.Trim();
                     dic.List_Vocab = Regex.Replace(regrex,@"<!--[^>]*--!>", String.Empty);
                     
                     lst.Add(dic);
                 }



    It's on listbox is group. SelectedIndex are always equal zero.Ex:

    th1
    th2
    Taping are always selectedIndex=0;

    I want on listbox is every own items. Ex: tap th1 then selectedIndex =0 else th2 then selectedIndex=1

    Other issue IS I want delete this passage is "<!--Todo: add links and weight-->"

    Friday, January 16, 2015 6:17 AM

Answers

  • Also paste here the code you have written for getting selected index.

    http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8

    Friday, January 16, 2015 6:34 AM
  • The code seems to be fine, have you seen in debugger as no matter on which item you click the selectedIndex remains 0? 

      var selindex = lst_vocab.SelectedIndex;

    See in debugger what does selindex returns when clicked on different items.

    Note/Suggestion : Please do not mark every reply as answer, mark only when you have got the final solution for the question asked.


    http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8


    • Edited by Vineet24 Friday, January 16, 2015 9:32 AM
    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:03 AM
    Friday, January 16, 2015 9:31 AM
  • Hi,

    The problem is, You are adding only one item so it always returns SelectedIndex as 0.  

    i) Have a List (may be a List<string>)

    ii) Add your items ("th1" and "th2" separately)

    iii) Now, bind your ListBox ItemsSource with the List



    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:03 AM
    Friday, January 16, 2015 9:46 AM
  • Here that results but it is subject because of when tapping will return selectedindex 0. I have still delete <!--Todo: add links and weight-->


    Hi,

    You source pattern and regex pattern doesnt match, so the comment is not removed

    Use this,

     String result = Regex.Replace(str, @"<!--[^>]*-->", String.Empty);


    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:25 AM
    Friday, January 16, 2015 10:14 AM
  • I want to slipt it. Making every items.

    A sample pseudo-code:

        lst.Add(dic.Item1);
        lst.Add(dic.Item2);
        lst.Add(dic.Item2);


    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:35 AM
    Friday, January 16, 2015 10:22 AM
  • Im trying that but still don't work.

    String regrex = div.SelectSingleNode(".//ul[@class='cdo-cloud']").InnerText.Trim();
                     dic.List_Vocab = Regex.Replace(regrex,@"<!--[^>]*--!>", String.Empty);

    Hi,

    Post what data you are having in  dic.List_Vocab


    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:38 AM
    Friday, January 16, 2015 10:32 AM
  • It is 1 object. Couldn't slipt such that
    You need to split using some token, may be "-" or "," or something. See String Split

    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 11:25 AM
    Friday, January 16, 2015 10:38 AM
  • It like 1 photos. So can't slipt
    What is its datatype ? Is the List_Vocab contains all your ListBox Items ?

    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:56 AM
    Friday, January 16, 2015 10:41 AM

All replies

  • Also paste here the code you have written for getting selected index.

    http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8

    Friday, January 16, 2015 6:34 AM
  • private void lst_vocab_SelectionChanged(object sender, SelectionChangedEventArgs e)
             {
                 if (lst_vocab.SelectedIndex > 0)
                 {
                     NavigationService.Navigate(new Uri("Pages2.xaml?l=" + lst_vocab.SelectedItem, UriKind.RelativeOrAbsolute));
                 }
                 else
                     return;
    
             }

     <ListBox Name="lst_vocab" SelectionChanged="lst_vocab_SelectionChanged">
                                        <ListBox.ItemTemplate>
                                            <DataTemplate>
                                                <StackPanel Orientation="Horizontal">
    
                                                    <TextBlock Text="{Binding List_Vocab}"
                                                                   FontSize="13"
                                                                   Foreground="Black"
                                                                   FontWeight="Thin"></TextBlock>
                                                </StackPanel>
                                            </DataTemplate>
                                        </ListBox.ItemTemplate>
                                    </ListBox>
                                </Grid>


    Friday, January 16, 2015 9:21 AM
  • The code seems to be fine, have you seen in debugger as no matter on which item you click the selectedIndex remains 0? 

      var selindex = lst_vocab.SelectedIndex;

    See in debugger what does selindex returns when clicked on different items.

    Note/Suggestion : Please do not mark every reply as answer, mark only when you have got the final solution for the question asked.


    http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8


    • Edited by Vineet24 Friday, January 16, 2015 9:32 AM
    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:03 AM
    Friday, January 16, 2015 9:31 AM
  • Hi,

    The problem is, You are adding only one item so it always returns SelectedIndex as 0.  

    i) Have a List (may be a List<string>)

    ii) Add your items ("th1" and "th2" separately)

    iii) Now, bind your ListBox ItemsSource with the List



    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:03 AM
    Friday, January 16, 2015 9:46 AM
  • Hi Greg,

    Like Vineet said, do not mark every reply as answer. It will create lot of confusion to people who are reading this thread in future. I strongly recommend you to follow this in future threads.


    Pradeep AJ

    Friday, January 16, 2015 10:01 AM
  •  private async void Content_pages_dictionary(string msg)
            {
                url_string = await getstring.GetAsync(msg);
                HtmlDocument html = new HtmlDocument();
                html.LoadHtml(url_string);
                var doc=html.DocumentNode.SelectNodes("//div[starts-with(@class,'responsive_cell_center')]");
                List<Assets.Information_Content> lst = new List<Assets.Information_Content>();
                foreach(var div in doc)
                 {
                     
                     dic.Title = div.SelectSingleNode("//h1").InnerText.Trim();
                     string regrex = div.SelectSingleNode(".//ul[@class='cdo-cloud']").InnerText.Trim();
                     dic.List_Vocab = Regex.Replace(regrex,@"<!--[^>]*--!>", "");
                     dic.h2 = "Favourite Entries";
                     txt_h2.Text = dic.h2.ToString();
                     txt_title.Text = dic.Title.ToString();
                     lst.Add(dic);
                 }
                lst_vocab.ItemsSource = lst;
            }
    Im using http agility pack. This code get all of <li> tags. That meaning is getting subject type.
    Friday, January 16, 2015 10:05 AM
  • Here that results but it is subject because of when tapping will return selectedindex 0. I have still delete <!--Todo: add links and weight-->


    Friday, January 16, 2015 10:07 AM
  • I want to slipt it. Making every items.
    Friday, January 16, 2015 10:09 AM
  • Here that results but it is subject because of when tapping will return selectedindex 0. I have still delete <!--Todo: add links and weight-->


    Hi,

    You source pattern and regex pattern doesnt match, so the comment is not removed

    Use this,

     String result = Regex.Replace(str, @"<!--[^>]*-->", String.Empty);


    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:25 AM
    Friday, January 16, 2015 10:14 AM
  • I want to slipt it. Making every items.

    A sample pseudo-code:

        lst.Add(dic.Item1);
        lst.Add(dic.Item2);
        lst.Add(dic.Item2);


    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:35 AM
    Friday, January 16, 2015 10:22 AM
  • Im trying that but still don't work.

    String regrex = div.SelectSingleNode(".//ul[@class='cdo-cloud']").InnerText.Trim();
                     dic.List_Vocab = Regex.Replace(regrex,@"<!--[^>]*--!>", String.Empty);

    Friday, January 16, 2015 10:29 AM
  • Im trying that but still don't work.

    String regrex = div.SelectSingleNode(".//ul[@class='cdo-cloud']").InnerText.Trim();
                     dic.List_Vocab = Regex.Replace(regrex,@"<!--[^>]*--!>", String.Empty);

    Hi,

    Post what data you are having in  dic.List_Vocab


    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:38 AM
    Friday, January 16, 2015 10:32 AM
  • It is 1 object. Couldn't slipt such that
    Friday, January 16, 2015 10:35 AM
  • It is 1 object. Couldn't slipt such that
    You need to split using some token, may be "-" or "," or something. See String Split

    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 11:25 AM
    Friday, January 16, 2015 10:38 AM
  • It like 1 photos. So can't slipt
    Friday, January 16, 2015 10:40 AM
  • It like 1 photos. So can't slipt
    What is its datatype ? Is the List_Vocab contains all your ListBox Items ?

    Pradeep AJ

    • Marked as answer by Le Thien Hoang Friday, January 16, 2015 10:56 AM
    Friday, January 16, 2015 10:41 AM
  • Yes. But I don't know

     public class Information_Content
        {
            public string Title { get; set; }
            public string List_Dictionary { get; set; }
            public string List_Vocab 
            { get; set; }
            public string h2 { get; set; }
            public string other_dictionaries { get; set; }
        }

    Friday, January 16, 2015 10:49 AM
  • All of text is listbox items =0
    Friday, January 16, 2015 11:00 AM
  • Must slipt out made every own items.
    Friday, January 16, 2015 11:03 AM
  • I had slipt. but it's just show 1 item.

     string regrex = div.SelectSingleNode(".//ul[@class='cdo-cloud']").InnerText.Trim().ToString();
                     string[] arrays = Regex.Split(regrex, "\r\n");
                    foreach(var t in arrays)
                    {
                        dic.List_Vocab = t.ToString();
                        
                    }
                     

    Friday, January 16, 2015 11:26 AM
  • I had slipt. but it's just show 1 item.

     string regrex = div.SelectSingleNode(".//ul[@class='cdo-cloud']").InnerText.Trim().ToString();
                     string[] arrays = Regex.Split(regrex, "\r\n");
                    foreach(var t in arrays)
                    {
                        dic.List_Vocab = t.ToString();
                        
                    }
                     

    Hi,

    Your datamodel should be,

    public class Information_Content
        {
            public string Title { get; set; }
            public string List_Dictionary { get; set; }
            public List<string> List_Vocab 
            { get; set; }
            public string h2 { get; set; }
            public string other_dictionaries { get; set; }
       public Information_Content()
       {
       List_Vocab=new List<string>();
       }
        }

    And now, split your strings and store it in the List,

    string regrex = div.SelectSingleNode(".//ul[@class='cdo-cloud']").InnerText.Trim().ToString();
                     string[] arrays = Regex.Split(regrex, "\r\n");
                    foreach(var t in arrays)
                    {
                        dic.List_Vocab.Add(t.ToString());
                        
                    }
    Then, set dic.List_Vocab as ItemsSource for your ListBox


    Pradeep AJ


    • Edited by Pradeep AJ Saturday, January 17, 2015 4:31 AM
    Saturday, January 17, 2015 4:30 AM