gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:52:01 26 4
gpt4 key购买 nike

我正在开发一个词汇应用程序,我想使用下一个和上一个按钮循环数组值。我从数据库和工作中分配了值。但我无法在数组值之间循环。增加工作量,但我必须单击几次才能循环以前的值。我找不到正确的递增、递减运算符。

 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;
}
}
});
}

最佳答案

试试这个

    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;

关于java - 我如何在Android中使用下一个和上一个按钮循环数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52100500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com