java - ArrayList 卡在打印一项
问题描述
我正在制作一个显示从 Firebase 提取并存储在 ArrayList 中的消息的应用程序。该应用程序应显示一条消息十秒钟,然后滚动消息以显示下一条消息。我正在使用 textSwitcher 视图来显示消息。我的问题是应用程序卡在 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 - mvn clean install 在新的本地机器上给出错误
- python - pandas:对具有每月特定常数的数据集应用每个月的计算
- wpf - 将数据网格绑定到powershell中的嵌套对象时如何更新UI
- python - Pyinstaller 将图像放在单独的文件问题?我该如何解决?
- java - 无法将 Java(Netbeans)连接到 MariaDB
- html - 使用 FlexBox Grid 更改菜单图标位置 HTML/CSS
- string - 在编译时获取`str`的字节
- azure-logic-apps - 为什么我在逻辑应用程序中的条件块后看到随机 ActionFailed
- python - MultiWorkerMirroredStrategy 在第一个纪元之前挂起?
- minizinc - 在 MiniZinc 中支持带有决策变量的 pow()