首页 > 解决方案 > 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));
        }
    });

标签: javaandroidfirebase

解决方案


我发现了问题并修复了它。我没有更新 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();

推荐阅读