如何在Android中使用下一个和上一个按钮循环数组值

时间:2018-08-30 15:20:31

标签: java android

我正在开发一个词汇应用程序,我想用下一个和上一个按钮循环数组值。我从数据库分配了值并正在工作。但是我不能在数组值之间循环。越来越多的工作,但我不得不单击几次以循环以前的值。我找不到正确的增量,减量运算符。

 int position = 0; 
`btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(position >= array.length){
                position = 0;
                txtWord.setText(array[position][0]);
                position++;                    
            }else {
                txtWord.setText(array[position][0]);
                position++;                   
            }
        }
    });`

btnBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(position >= 0){
                txtWord.setText(array[position--][0]);
            }else{
                position = array.length;
                txtWord.setText(array[position--][0]);
            }

        }
    });

完整代码在这里:

DatabaseHelper myDB;
TextView txtWord, txtNumberOfWords;
Button btnBack, btnShow, btnNext;
String[][] array;
int position = 0;
boolean showWord= true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);

    txtWord = (TextView)findViewById(R.id.txtWord);
    txtNumberOfWords = (TextView)findViewById(R.id.txtNumberOfWords);
    btnBack = (Button)findViewById(R.id.btnBack);
    btnShow = (Button)findViewById(R.id.btnShow);
    btnNext = (Button)findViewById(R.id.btnNext);

    myDB = new DatabaseHelper(this);


    Cursor data = myDB.getListContents();
    array = new String[data.getCount()][2];

    if(data.getCount() == 0){
        Toast.makeText(MainActivity.this, "There is no definition!", Toast.LENGTH_LONG).show();
    }else{

        data.moveToFirst();

        for(int i = 0; i < data.getCount(); i++){
                for(int y = 0; y < 2; y++){
                    array[i][y] = data.getString(1+y);
                }
                data.moveToNext();
            }
        txtWord.setText(array[0][0]);
        position = position + 1;
        txtNumberOfWords.setText(position  + " of " + array.length);
    }

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(position >= array.length){
                position = 0;
                txtWord.setText(array[position][0]);
                position++;
                txtNumberOfWords.setText(position  + " of " + array.length);
            }else {
                txtWord.setText(array[position][0]);
                position++;
                txtNumberOfWords.setText(position  + " of " + array.length);
            }
            showWord = true;
        }
    });

    btnBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(position >= 0){
                txtWord.setText(array[position--][0]);
                txtNumberOfWords.setText(position  + " of " + array.length);
            }else{
                position = array.length;
                txtWord.setText(array[position--][0]);
                txtNumberOfWords.setText(position  + " of " + array.length);
            }

        }
    });

    btnShow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(showWord){
                txtWord.setText(array[position-1][1]);
                showWord = false;
            }else{
                txtWord.setText(array[position-1][0]);
                showWord = true;
            }
        }
    });
}

1 个答案:

答案 0 :(得分:1)

尝试一下

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            position = (position + 1) % array.length;
            txtWord.setText(array[position][0]);
        }
    });

    btnBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            position = (position - 1) % array.length;
            txtWord.setText(array[position][0]);
        }
    });

还有一个错误,因为您同时将position用作 TextView -txtNumberOfWords和数组指针。那些是不同的。对于TextView,显示position+1,对于数组,使用position。不要忘记从以下位置更改代码行:

position = position + 1;

对此:

position = 0;