GWT CellTable getRowElement throwing IndexOutOfBoundsException - java

I am using a CellTable in GWT and trying to add styling to a row whenever certain events happen. The code to add styling is as follows:
Range range = playlistTable.getVisibleRange();
int start = range.getStart();
for (int i = start; i < start + playlistTable.getPageSize(); i++) {
if (playlistData.get(i) == currentSong) {
playlistTable.getRowElement(i).addClassName("highlightRow");
} else {
playlistTable.getRowElement(i).removeClassName("highlightRow");
}
}
When the app loads and the first page of the cell table is displayed, everything works perfectly. However when I scroll to the next page and make a call to the above code, GWT throws an IndexOutOfBoundsException. It does not like my index when I make the call to getRowElement, which only occurs if the index is "not on the current page." It's as though getRowElement always thinks the current page is the first page. If I scroll back to the first page, everything behaves fine. Has anybody encountered this? Am i missing something or is this a bug in GWT?

I suspect the problem is that start is 0 on the page that works. What happens if you do playlistTable.getRowElement(i - start) ? You may have to throw a + 1 or a - 1 in somewhere.

Not a direct answer to your question, but your code would be much simpler with RowStyles
// do it only once!
playlistTable.setRowStyles(new RowStyles<Song>() {
#Override
public String getStyleNames(Song row, int rowIndex) {
return (row == currentSong) ? "highlightRow": "";
}
});
You might also want to use equals() and/or some ProvidesKey object to compare the row with the currentSong.

Related

java selenium how to use if else

There is a table with application numbers and user data. at the beginning of the table, the application number at the end is a green arrow. There can be an infinite number of fields in a table and it always changes.
I need to find the green arrow by the application number and click on it accordingly.
tried to do something like
String myorder = "629/0000/000000021059";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div/div[1]/table/tbody/tr/td[5]\n")).getText())){
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[1]/table/tbody/tr/td[18]/a/img\n")).click();
} if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/table/tbody/tr/td[18]/a/img\n")).click();
} if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[3]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[3]/table/tbody/tr/td[18]/a/img\n")).click();
and so on up to 100+
The first problem is that I really don't like the option of writing if else 100 times. And there is only one digit in the diva where the id of the application and one in the digit in the diva where the green arrow is different(img)
The second problem is that no matter how I insert break; when finding the right application, he clicks on the desired arrow, but after apparently trying to continue searching the page, but the page has already changed and the error falls
(WARNING: The server did not provide any stacktrace information)
if I use the code that was attached above or just an indication of the element (which goes right after the one that turned out to be correct and the page has changed) with a note that I can not find it, if I use else if with break;
tried to do so
for (int i = 1; i < 25; i++) {
String myorder = "629/6300/000000412067";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div["+ i++ +"]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div["+ i++ +"]/table/tbody/tr/td[18]/a/img\n")).click();
break;
}
But if the item is located, then it clicks on the very first arrow in the table, and not on the one in the same column as the application number
I would really appreciate your help!
XPaths are 1-based not 0, so you are correct in thinking you need to add one, however, these two are not equal as i++ modifies the value of i (even inline like you have it), which you do twice inside your loop. Therefore the 2nd iteration of your loop i will be 3, not 1 as expected. In your loop replace i++ with i + 1 and you should be good to go.
for (int i = 1; i < 25; i++) {
String myorder = "629/3500/000000329976";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[" + i + 1 + "]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[" + i + 1 + "]/table/tbody/tr/td[18]/a/img\n")).click();
break;
}
}
Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[31]/table/tbody/tr/td[5]
"}
So it was searching untill 31. And it's strange because i used for i < 25 and there are only 25 rows in the table.
but still dont work for me;(

How to alter and display only one column (via user input) on 2D array Java

I am creating a game board. I need it to reveal a the selected column after a user input, while the rest of the columns still print as "X". This game holds values I have set in each column, but does not print them on the screen. When the user selects a column, I need it to print showing the value that column is holding while the rest of the columns still print "X" so they do not reveal what they have. I am new to this, thank you for your help.
This is the function where I think the problem is. If you look, you will see that I have the if statement "if (isCovered) - then I want it to print the covered version. Then the "else" - which is where I want it to print just the one that was guessed as its actual value. I have tried multiple ways of achieving this with no luck. Is there are way to make it like (!isCovered)? But that doesn't work, because it states it needs to be an array and the function "!" does not work. Right now it just seems like it never prints the "else" statement at all. I have functions that take the user input and compare them to "isCovered" and they work correctly, because the piece moves on the board as it should. I just cannot get it to print the actual value instead of an "X". Thank you for any help and if further information would be helpful, please let me know. It is due today unfortunately I only had a few days to work on it and have been working constantly on it.
public static void PrintRevealBoard(int[][] myArray,Boolean[][] isCovered)
{
int i, j;
for (i = 0; i<myArray.length ; i++ ) { // array.length = max rows
System.out.print((i+1) + " ");
for(j = 0; j <myArray[0].length; j++) { // array[0].length = max
cols
if(isCovered[i][j]){
System.out.print(GetRollColorCovered(myArray[i][j]) + " ");
} else {
System.out.print(GetRollColor(myArray[i][j]) + " ");
}
}
your main module is kinda messy. And I don't know how GetRollColor(dice) works. Anyway as I understand you have a two dimensional array and you want to show only a specific value. Seems like u want to show the entire input column.
use this to update isRevealed() after the input of inputCol.
public static Boolean[][] updateRevealed(Boolean[][] isRevealed, int inputCol){
for(int i=0;i<isRevealed[inputCol].length;i++)
isRevealed[inputCol][i] = true;
return isRevealed;
}
update like this,
isRevealed = updateRevealed(isRevealed,inputCol);
your printRevealBoard is almost correct. Just remove the first line. It doesn't make sense and you don't want it as I see
int isRevealed = inputCol;
I don't know how your array looks like. But because of the first for loop u will definitely get an
index out of bounds exception
loop runs until I becomes myarray.length. and in the next loop you access index I of myArray. Exception will be thrown if I=myArray.length. u gotta fix it. If any problem occurs lemme know.
thankyou
edit:
try this for printRevealBoard
public static void printRevealBoard(char[][] myarray , Boolean[] []isRevealed){
for(int i=0;i<myarray.length;i++){
for(int j=0;j<myarray[0].length;j++){
if (isRevealed[i][j]) System.out.print(myArray[i][j] + " ");
else System.out.print("* ");
}
System.out.println();
}
}

How to get the number of rows that contain a certain word?

So i have this code-generated table and i've been trying to get this code to work, basically i want to know how many rows have the word so i can later work with that number of elements, this code is like not working maybe you guys can see the error?
Code:
public int UntilArraySearch (String filtro){
int tope=0;
for(int i = 0;i<mTableLayout.getChildCount();i++){
if(data[i].productName.contains(filtro) || data[i].productCode.contains(filtro))
tope++;
}
return tope;
}
And this error pops up:
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=52; index=52
I get this error whenever i run the function
Your loop would work only if mTableLayout.getChildCount() is <= data.length and it seems that it is not, because if it was the case then i would not exceed the max index for data.
Since you want to iterate through data, why don't you write the loop like this:
for(int i = 0;i<data.length;i++){
if(data[i].productName.contains(filtro) || data[i].productCode.contains(filtro))
tope++;
}
and check if this is the expected result?
If you are searching through data, then you might as well iterate over the length of the data array. Whether or not your view (mTableLayout) properly reflects the model (data), this operation can better isolate itself from the view by only considering the model which is a good practice to follow in general.
public int UntilArraySearch (String filtro){
int tope=0;
for(int i = 0;i<data.length;i++){
if(data[i].productName.contains(filtro) || data[i].productCode.contains(filtro))
tope++;
}
return tope;
}

Unexpected behavior with ArrayList.remove()

I have a pizza code that iterates through a list of objects and checks whether they are colliding or not. If one is collided with, it is removed from the ArrayList.
for (int i = 0; i < arrayList.size(); i++) {
Object c = arrayList.get(i);
if (Rect.intersects(foo.getRect(), c.getRect())) { //Android function, checks if the two rectangles are inside each other.
foo.setPosY(c.getPosY() + 11); // Always works.
foo.setVelY(bird.getVelY() * -1); // Always works.
arrayList.remove(i); // Occasionally fails under special circumcisions.
}
}
When opening the app fresh for the first time this works fine. However, if I exit with the back button and then quickly reopen the app, it will all work except occasionally, removing the object from the ArrayList wont happen.
If I close the app and then remove it from the recent apps list, it works. If I exit with the home key, or the back button and then immediately reopen, it sometimes fails to remove the object. I don't understand why this happens, as everything else still works. The two lines of code just before it function just fine. I just don't get it.
I suspect the problem is that you're skipping an element after one call to remove, because you're incrementing i and everything in the list is moving up one element. (It's not really clear what the symptoms are at the moment. If this turns out not to be the problem, it's still a problem.)
So if you call remove(2), the next element you want to look at now has index 2.
Common ways of fixing this:
Work backwards:
for (int i = arrayList.size() - 1; i >= 0; i--)
Use an iterator instead:
for (Iterator<Foo> iterator = arrayList.iterator(); iterator.hasNext(); ) {
Foo c = iterator.next();
if (...) {
iterator.remove();
}
}

Obtaining current tab of ViewPager in Android [duplicate]

I know with the Gallery widget I was able to use getSelectedItemPosition(); to retrieve the current position, however it doesnt seem ViewPager has that.
I know I can setup a listener and retrieve the position when the page is switched. But I want the current view position.
You can use:
mViewPager.getCurrentItem()
Create a listener and set it on your viewpager:
/**
* Get the current view position from the ViewPager by
* extending SimpleOnPageChangeListener class and adding your method
*/
public class DetailOnPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
private int currentPage;
#Override
public void onPageSelected(int position) {
currentPage = position;
}
public final int getCurrentPage() {
return currentPage;
}
}
Update 2019
Now you can set addOnPageChangeListener on View Pager to Observe change in Page position.
Since you wanted to setup a listener and retrieve the position when the page is switched
mViewPager.addOnPageChangeListener(object : OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {
pagePosition.setText("" + position + "/" + galleryAdapter!!.count)
}
})
My solution would only work if you have a TabLayout linked to your ViewPager.
This is how they're linked:
tabLayout.setupWithViewPager(viewPager);
And then to get current position, you can make use of:
tabLayout.getSelectedTabPosition()
I'm telling you now its a hack, so there is no reason to downvote for that reason. Meaning, it either will be helpful to you specifically or not. Either way, the description below will provide some insight and be helpful to the community. Also, this solution is good for the older APIs that don't have ViewPager.getCurrentItem().
First, a little information. If you iterate thru all children of a ViewPager with ViewPager.getChildAt(x); and print out with toString() (or getLeft()) each child View (a page) and then do this everytime you change pages, you will notice that the children will not be in the logical order that they are displayed in when you start going back pages (paging back to the beginning). Apparently, it will remove the unneeded child from the array then append the newest child to the array. So, for example, lets say you are looking at page 2 then changed to page 3, your list of children will be in this order page 2, page 3, page 4 meaning that ViewPager.getChildAt(1); will return the current page. But, if you then change back to page 2 (from page 3) your list of children will be in this order page 2, page 3, page 1 which means that ViewPager.getChildAt(1); does not return the current page. I have not yet been able to find simple logic to weed out the current page using this information. Because the order of the pages in the array behind getChildAt is in an arbitrary order based off of how the user has been paging around.
That being said, I developed a hack work-around. I have no clue if this function will work in all in environments, but it works for my current project. I would suspect if doesn't for you, then its an issue of different API level. But I dont actually suspect any issues for other environments.
Now, onto the meat. What I noticed was that the result of ViewPager.getChildAt(x).getLeft() will have some type of horizontal pixel coordinate relative to the parent. So, I used this information to weed out which view is the current one.
private int getCurrentPageIndex(ViewPager vp){
int first,second,id1,id2,left;
id1 = first = second = 99999999;
View v;
for ( int i = 0, k = vp.getChildCount() ; i < k ; ++i ) {
left = vp.getChildAt(i).getLeft();
if ( left < second ) {
if ( left < first ) {
second = first;
id2 = id1;
first = left;
id1 = i;
} else {
second = left;
id2 = i;
}
}
}
return id2;
}
This function is probably a questionable hack because it relies on the value of getLeft() to figure it all out. But, I grab the left coordinate of each child. I then compare this to the other values and store the first and second pages, returning the second page (current page) out of the function. It seems to work beautifully.
Why (might you ask) didn't I just use onClickListenter or whatever solution? Well, I was darned determined that there was a straight forward way to do this without having to include listeners, other classes, inconclusive focus, and other bloat. Unfortunately, this solution is not exactly straight forward. But, it does do away with the bloat, other classes and listeners. If I can figure out a more straight forward way, I will be rewriting this function. Or maybe, this will provide insight for someone else to have an epiphany.

Categories