0

I have a listbox and it's source is Binded to a XmlDataProvider. and a RSS link for the source of XmlDataProvider. It's all working correctly, getting feeds and displaying them in the listbox. I have a DispatcherTimer in the code that Refresh the XmlDataProvider source every 10 minutes. Now All I need is to count the new items added in the ListBox in each Refresh Interval.

Can anyone help me to implement a way to count only new items added in the listbox in each Refresh Time Interval? Please Help.

D.K.
  • 396
  • 3
  • 6
  • 21

1 Answers1

0

I agree with "before adding the item to list count it. – Shujaat Siddiqui", but also you cold add Linq or For cicle, to check if ID of the Item already exists, and count the unique ones. Something like:

int NewItemsCount = 0;
for (int i = 0; i<XmlDataProvider.Items.Count; i++)
{
    bool IsOld=false;
//Loop throu all items in existing
    for (int o =0; i<NewData.Items.Count;i++)
    {
        if(XmlDataProvider.Items[i].ID==NewData.Items[o].ID)
        {
            IsOld = true;
            break;
        }
    }
    if(!IsOld)
    {
         NewItemsCount++;
    }
}

More detailed:

First time when you get the news, you do something like this:

For example you have some model for your news:

class Item
{
    string guid;
    string title;
    ...
}

And you get your news like:

List news = XML.Deserialize(response.GetResponse()) // can't remember by memory, but this is your response from server deserialized using XML deserializer

Then you populate your ListBox with List news, like this:

 ListBox.DataSource = news OR using for/forin and ListBox.Items.Add();

Now you get the update from the server:

List news = XML.Deserialize(response.GetResponse()) // can't remember by memory, but this is your response from server deserialized using XML deserializer

Now you should check how much new items were added ( before adding new items to ListBox ), you should do something like:

a) if you used ListBox.DataSource = news

 List<Item> OldNews = (List<Item>)ListBox.DataSource;
 int newUniqueNewsCount = 0;
 foreach ( Item newObj in news ) // loop through new items that you just got as update from server
 {
      bool IsOld = false;
      foreach ( Item obj in OldNews) // loop through old items
      {
           if(obj.guid==objNew) //check if this GUID already existed
           {
               IsOld = true;
               break; //end the looping
           }
      }
      if(!IsOld)
      {
           // If code ran in here then this GUID is new and then this news is new so +1
           newUniqueNewsCount ++;
      }
 }

After this code ran you can use newUniqueNewsCount to show the new items count in UI.

Developer
  • 4,158
  • 5
  • 34
  • 66
  • I really don't remember how to access the Items in data provider, because i'm not on Visual Studio right now, but it must be simple. Or you could loop throu items in listbox as another choice. – Developer Sep 04 '14 at 06:19
  • items in the data provider loks something like this. there's no IDs to access. http://prntscr.com/4jjczx Is there any way to use the DataTime as variable? – D.K. Sep 04 '14 at 09:40
  • any item in the list box should have something unique that you can attahc to, ID was as an example – Developer Sep 04 '14 at 14:32
  • I can't quite find anything like an ID in this one. please check the screen shot. http://prntscr.com/4jjczx – D.K. Sep 07 '14 at 03:11
  • 1
    you have property - it is Globally Unique Identifier so... In this examples really the numbers after '/' will be unique for each notification. So that means you can check old items and new items if the item with this guid already existed in you'r list box – Developer Sep 08 '14 at 05:43
  • Damn...how did I missed a simple thing like this. Thanks @Cheese – D.K. Sep 10 '14 at 02:23
  • I'm having a hard time implementing this. I get it what you said about the id. still since I'm new to WPF, can you please be a little more specific about the newitem count thing. please? – D.K. Sep 11 '14 at 18:13
  • Added more detailed explanation – Developer Sep 15 '14 at 08:03