How to get the top 7 items in listbox - listbox

how can I remove the top 7 items in a listbox?
int end = Count + 7;
for (int i = Count; i < end; i++)
{
page.nuus.Items.Add(page.local_story_list[i]);
}
this happens when the bottom of a scroller is reached, then the latest 7 items gets added to the list, however I want to remove the previous 7 items before this occurs?

You can remove the first item 7 times.
for (int i = 0; i < 7; i++)
{
if (page.nuus.Items.Count > 0)
{
page.nuus.Items.RemoveAt(0);
}
}

Related

dart: changing list[i] changes list[i-1] too

I have a List of the type Model. when I loop all its elements and loop the next one except for the last one, then change the last one manually, the one before changes.
here is a little code to reproduce the problem (also you can run it directly in dartpad from here)
void main() {
List<Model> s = [];
for (int i = 0; i < 5; i++) {
s.add(Model(i));
}
for (int i = 0; i < s.length - 1; i++) {
s[i] = s[i + 1];
}
print(s);
s[s.length-1].x = 100;
print(s);
}
class Model {
int x;
Model(this.x);
#override
String toString() => 'x: ' + this.x.toString();
}
notice that this problem does not happen when you comment out the for loop or the manual change, or instead of changing the last one's property, you reassign a new value to it, like s[s.length - 1] = Model(100);. seems like dart for some reason is re-running the loop.
When you run the second for loop, you assign the i + 1th Model to the ith position in the list:
// initialise list
for (int i = 0; i < s.length; i++) {
s[i] = s[i + 1]
}
If you unwrap the loop, it looks roughly like this:
s[0] = s[1];
s[1] = s[2];
s[2] = s[3];
s[3] = s[4];
Notice that this leaves s[4] unchanged, but also assigns it to s[3].
In Dart, variables contain references to objects. This means that when your list runs s[3] = s[4];, both s[3] and s[4] point to the same object.
Now, if you modify s[4] you, are actually modifying the objects that s[4] refers to, which happens to also be the object that s[3] refers to, so they both appear to change in your list.

How To Set Addapted PassData Between Activities?

i am trying to create a dynamic list of buttons and every button create a passed data to another activity that show the relative buttons detile.
the code i used is here:
for (int i = 0; i < size; i++)
{
temp = new Button(this);
temp.SetText(data[i].Name, TextView.BufferType.Normal); //arbitrary task
numArrey[i] = i;
main_linearScroller.AddView(temp);
tv[i] = temp;
tv[i].SetTextSize(Android.Util.ComplexUnitType.Dip, 20);
tv[i].SetBackgroundResource(Resource.Drawable.textbox_back1);
Display d = WindowManager.DefaultDisplay;
int width = d.Width;
int height = d.Height;
tv[i].SetHeight(10);
tv[i].SetWidth(width);
tv[i].Click += (sender,e)=>
{
var detileListShow = new Intent(this, typeof(DetileListShow));
detileListShow.PutExtra("Data", i);
Console.WriteLine("Starting Activity With Data{0}", i);
StartActivity(detileListShow);
};
every thing is good untile last parag that i setting tv[i].click.
every buttons.PutExtera = 21.
how can i fix it?
i think it's better every buttons can save the value that need to return but i dont know how...

Code Unreachable Error in Code As Warnng

if(Screens.Count > 0)
{
for (int i = Screens.Count -1; i < 0; i +=1)
{
if (Screens[i].GrabFocus)
Screens[i].GrabFocus = true;
break;
}
}
Unreachable Code
Trying to convert
VB code to C#
For I = FoundScreens - 1 to 0 step -1
if (Screens[i].GrabFocus)
Screens[i].GrabFocus = true;
break;
Where is my mistake in C#?
At first glance the VB code starts with the last screen in the list to the first one
VB.NET
For I = FoundScreens - 1 to 0 step -1
To perform the same thing in C#
for (int i = Screens.Count -1; i > 0; i--)
Your original code, was unreachable, because of the loop condition in the for, which you implemented as such:
for (int i = Screens.Count -1; i < 0; i +=1)
i< 0, when i is assigned the number of screens it can be 0 to n, so i will never be smaller than 0.
I'm surprised that you would get such a warning in the for because a simple test of this code
for (int i = 0; i < 0; i +=1)
{
Console.WriteLine(i.ToString());
}
Console.ReadKey();
Will show no warnings in a simple console application.

Google-esque Pagination with Razor

I currently have pagination functionality that displays the total page count, but I was wondering how I can have page links to display similar to Google search results? Essentially only showing 10 total links at a time, and if the current page is greater than 7, the first link displayed will be currentPageIndex - 5.
This is the current Razor/html that I have that displays the number of links equal to the total page count:
#for (int i = 0; i < Model.PageCount; i++)
{
if (Model.CurrentPageIndex == i)
{
<li id="page#(i)" class="disabled">#(i + 1)</li>
}
else
{
<li id="page#(i)">#(i + 1)</li>
}
}
This seems like more of a logic problem than a razor problem.
#{
int pagesDisplayed = 10;
int firstPage = Model.CurrentPageIndex - pagesDisplayed / 2;
if(firstPage < 0){
firstPage = 0;
}
}
#for (int i=firstPage; i <= (firstPage + pagesDisplayed); i++){
if (Model.CurrentPageIndex == i)
{
<li id="page#(i)" class="disabled">#(i + 1)</li>
}
else
{
<li id="page#(i)">#(i + 1)</li>
}
}

Actionscript randomly distribute objects on stage

I'm trying to distribute 3 objects randomly on my stage but it's not working. My movie is 800x800.
function makeRock():void{
var tempRock:MovieClip;
for(var i:Number = 1; i < 3; i++){
tempRock = new Rock();
tempRock.x = Math.round(800);
tempRock.y = Math.round(-800);
addChild(tempRock);
}
}
What am I doing wrong?
Replace Math.round(800); with Math.random()*800;
function makeRock():void
{
var tempRock:MovieClip;
var i:uint = 0;
for(i; i < 3; i++)
{
tempRock = new Rock();
tempRock.x = Math.random()*800;
tempRock.y = Math.random()*800;
addChild(tempRock);
}
}
Math.round(800) is just returning 800.
Math.random() returns a random number between 0 and 1, which you can multiply by 800 to get a random result of 0-800. A good note to make is that Math.random() never actually returns 1.0. Just everything from 0 up to 1.
Further reading:
As a side note: this makes it simple to return a random element from an array; because you're never getting 1 you can cast the result of Math.random()*array.length to uint() and always be within the boundaries of the array length.
eg.
var ar:Array = [1,2,"hello",4,5,6,7,8,9,0];
var randomElement:Object = ar[uint(Math.random()*ar.length)];
trace(randomElement);

Resources