简体   繁体   中英

How to press a button, move to the next position of an array

I'm starting to program in android and I'm a difficulty. I'm filling an ArrayList seeking a database object.

 public ArrayList<Box> seekBoxPerLine() {
        boxes = new ArrayList<Box>();
        Cursor cursor;
        if (init == 0) {
            String sql = "SELECT * FROM Boxe WHERE line='" + numberLine+ "' ORDER BY position ASC; ";
            cursor = database.rawQuery(sql, null);
        } else {
            String sql = "SELECT * FROM Box WHERE line='" + numberLine + "' ORDER BY positio DESC; ";
            cursor = database.rawQuery(sql, null);
        }
        if (cursor.getCount() >= 0) {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                Box box = new Box();

                box.setId(cursor.getLong(0));
                box.setFase_cultivo(cursor.getString(1));
                boxes.add(box);
                cursor.moveToNext();
            }
        }
        cursor.close();
        return boxes;
    }

I do a verification, and pass the id of my object to a EditText:

  public void verifyBoxLine() {
        if (numberLine != null) {
            boxes = seekBoxPerLine();
            for (Box boxesPerLine : boxes) {
                etNumberBoxActually.setText(String.valueOf(boxesPerLine.getId()));

                break;
            }
        }
    }

Now my doubt is how to program the button to press it to him increment the same EditText with the next arrayList ID? I have no idea any help is welcome.

My onClick button:

  public void onClick(View view) {
 if (view == btNextBox) {


        }

notes: The boxes are positioned in a line sequentially and I can choose to start at the beginning or the end of the line, so i realize the checks, do not know how much it helps but it would be to make it clearer.

why don't you use an internal counter?

whenever you query from the DB you reset the counter to 0;

whenever you click on the button, you increase the counter...

private int indexPos = 0;

public ArrayList<Box> seekBoxPerLine() {
    //... as above
    indexPos = 0;
}

public void onClick(View view) {
    if (view == btNextBox) {
        indexPos = indexPos + 1;
        //... do whatever you need to do
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM