我正在制作一个应用程序,用于显示从 Firebase 提取并存储在 ArrayList 中的消息。应用程序应显示一条消息十秒钟,然后滚动消息以显示下一条消息。 我正在使用 textSwitcher View 来显示消息。我的问题是应用程序卡在 ArrayList 中的最后一条消息处,并且不会从数组的开头再次启动。 预先感谢您。
这是位置的代码:
public class ViewMessageActivity extends AppCompatActivity {
private TextSwitcher txtSwitcher;
private ImageButton btnNext,btnPrev;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference notebookRef = db.collection("Messages");
ArrayList<String> ar = new ArrayList<String>();
int count = ar.size();
int position = 0;
TextView hallName;
这是负责此操作的代码:
if(!ar.isEmpty()) {
txtSwitcher.setText(ar.get(0));
count = ar.size();
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
position++;
if (position >= count)
position = 0;
txtSwitcher.setText(ar.get(position));
}
});
btnPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtSwitcher.showPrevious();
--position;
if (position < 0)
position = ar.size() - 1;
txtSwitcher.setText(ar.get(position));
}
});
最佳答案
我发现了问题并解决了它。我没有更新 ArrayList 中的位置。菜鸟错误。
代替此代码:
if(!ar.isEmpty()) {
txtSwitcher.setText(ar.get(0));
count = ar.size();
我这样写:
if(!ar.isEmpty()) {
txtSwitcher.setText(ar.get(position));
position++;
if (position >= count)
position = 0;
count = ar.size();
关于java - ArrayList 卡在打印一项上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61749698/