无效的列表操作:索引0,大小0。

huangapple go评论75阅读模式
英文:

Invalid list operation: index 0, size 0

问题

这是我在编辑文本更改时的代码(问题出现在我想要为listview(publicapps)进行搜索时)

我该怎么做来修复这个错误... 问题出现在我键入与 _charSeq 不相等的内容时

据我了解,在地图(pubmap)删除位置时会产生错误

if (stris.equals("p")) {
    if (Searchby.equals("1")) {
        progressbar6.setVisibility(View.VISIBLE);
        pub.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot _dataSnapshot) {
                pubmap = new ArrayList<>();
                try {
                    GenericTypeIndicator<HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap<String, Object>>() {};
                    for (DataSnapshot _data : _dataSnapshot.getChildren()) {
                        HashMap<String, Object> _map = _data.getValue(_ind);
                        pubmap.add(_map);
                    }
                }
                catch (Exception _e) {
                    _e.printStackTrace();
                }
                if (_charSeq.length() > 0) {
                    n3 = pubmap.size() - 1;
                    n4 = pubmap.size();
                    for(int _repeat84 = 0; _repeat84 < (int)(n4); _repeat84++) {
                        if (pubmap.get((int)n3).get("pubflair").toString().toLowerCase().contains(_charSeq.toLowerCase())) {
                            
                        }
                        else {
                            pubmap.remove((int)(n3));
                        }
                        n3--;
                    }
                }
                publicapps.setAdapter(new PublicappsAdapter(pubmap));
                progressbar6.setVisibility(View.GONE);
            }
            @Override
            public void onCancelled(DatabaseError _databaseError) {
            }
        });
    }
    else {
        progressbar6.setVisibility(View.VISIBLE);
        pub.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot _dataSnapshot) {
                pubmap = new ArrayList<>();
                try {
                    GenericTypeIndicator<HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap<String, Object>>() {};
                    for (DataSnapshot _data : _dataSnapshot.getChildren()) {
                        HashMap<String, Object> _map = _data.getValue(_ind);
                        pubmap.add(_map);
                    }
                }
                catch (Exception _e) {
                    _e.printStackTrace();
                }
                if (_charSeq.length() > 0) {
                    n3 = pubmap.size() - 1;
                    n4 = pubmap.size();
                    for(int _repeat146 = 0; _repeat146 < (int)(n4); _repeat146++) {
                        if (pubmap.get((int)n3).get("pubname").toString().toLowerCase().contains(_charSeq.toLowerCase())) {
                            
                        }
                        else {
                            pubmap.remove((int)(n3));
                        }
                        n3--;
                    }
                }
                publicapps.setAdapter(new PublicappsAdapter(pubmap));
                progressbar6.setVisibility(View.GONE);
            }
            @Override
            public void onCancelled(DatabaseError _databaseError) {
            }
        });
    }
}
英文:

Here's my code when edit text changed (the problem appeared after i wanted to make a search for listview(publicapps) using edittext)

What should i do to fix the error... The problem appears when i type something that is not equals the _charSeq

As i got it gives an error when the map (pubmap) deletes the position

if (stris.equals(&quot;p&quot;)) {
if (Searchby.equals(&quot;1&quot;)) {
progressbar6.setVisibility(View.VISIBLE);
pub.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot _dataSnapshot) {
pubmap = new ArrayList&lt;&gt;();
try {
GenericTypeIndicator&lt;HashMap&lt;String, Object&gt;&gt; _ind = new GenericTypeIndicator&lt;HashMap&lt;String, Object&gt;&gt;() {};
for (DataSnapshot _data : _dataSnapshot.getChildren()) {
HashMap&lt;String, Object&gt; _map = _data.getValue(_ind);
pubmap.add(_map);
}
}
catch (Exception _e) {
_e.printStackTrace();
}
if (_charSeq.length() &gt; 0) {
n3 = pubmap.size() - 1;
n4 = pubmap.size();
for(int _repeat84 = 0; _repeat84 &lt; (int)(n4); _repeat84++) {
if (pubmap.get((int)n3).get(&quot;pubflair&quot;).toString().toLowerCase().contains(_charSeq.toLowerCase())) {
}
else {
pubmap.remove((int)(n3));
}
n3--;
}
}
publicapps.setAdapter(new PublicappsAdapter(pubmap));
progressbar6.setVisibility(View.GONE);
}
@Override
public void onCancelled(DatabaseError _databaseError) {
}
});
}
else {
progressbar6.setVisibility(View.VISIBLE);
pub.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot _dataSnapshot) {
pubmap = new ArrayList&lt;&gt;();
try {
GenericTypeIndicator&lt;HashMap&lt;String, Object&gt;&gt; _ind = new GenericTypeIndicator&lt;HashMap&lt;String, Object&gt;&gt;() {};
for (DataSnapshot _data : _dataSnapshot.getChildren()) {
HashMap&lt;String, Object&gt; _map = _data.getValue(_ind);
pubmap.add(_map);
}
}
catch (Exception _e) {
_e.printStackTrace();
}
if (_charSeq.length() &gt; 0) {
n3 = pubmap.size() - 1;
n4 = pubmap.size();
for(int _repeat146 = 0; _repeat146 &lt; (int)(n4); _repeat146++) {
if (pubmap.get((int)n3).get(&quot;pubname&quot;).toString().toLowerCase().contains(_charSeq.toLowerCase())) {
}
else {
pubmap.remove((int)(n3));
}
n3--;
}
}
publicapps.setAdapter(new PublicappsAdapter(pubmap));
progressbar6.setVisibility(View.GONE);
}
@Override
public void onCancelled(DatabaseError _databaseError) {
}
});
}
} ```
</details>
# 答案1
**得分**: 0
首先,我建议您将您的代码划分为小的功能部分,因为当您遇到问题时,更容易找到错误。
显然,*bupmap* 是空的或者索引不存在(通过参数 "int"(n3)传递)。请问您能将堆栈错误信息在这里写出吗?
祝好!
<details>
<summary>英文:</summary>
first I recommended to you divide your code into small functionality cause when u have a problem is easier found an error. 
apparently *bupmap* is empty or not exist an index ( pass by params &quot; int)(n3) &quot;)..please can u write the stack error here? 
regards!!
</details>

huangapple
  • 本文由 发表于 2020年9月19日 01:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63959966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定