英文:
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("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) {
}
});
}
} ```
</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 " int)(n3) ")..please can u write the stack error here?
regards!!
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论