如何在循环外接收循环内创建的数据/值?
我确实在很多网站、博客和此处进行了搜索,但没有找到答案。
像这样:
public ArrayList<Dicionario> retornaArray() {
controlaBanco = new controlaBanco(this);//the classe with methods to manipule the data in a database
List<String> str = new ArrayList<>();
cursor = controlaBanco.carregaDados(); //this will give me the cursor with some data from a database(sqlite)
arr = new ArrayList<Dicionario>(); //an arraylist formated to hand my data and then put it in a personalized listview
int count = 0; //an number to control the data flow(it's 3000 linea of data and I guess my application might get slow by this
while (!cursor.isAfterLast() && count <= 1000) {
//here I take the data from columns separately
str.add(cursor.getString(0));
str.add(cursor.getString(1));
str.add(cursor.getString(2));
str.add(cursor.getString(3));
//here I take that data to formating this in my class
dicionario = new Dicionario(str.get(0), str.get(1), str.get(2), str.get(3));
//here I put my formated data into my formated array
arr.add(dicionario);
count++; //the count is upping...
}
//now I receive the array and returning this to my personalized adapter
return arr;
}
但它不起作用,因为它只会获取并返回第一行 :(
最佳答案
发生的事情是 str 为每次迭代添加了 4 个元素。然而,您从元素 0-4 中获取值(即第一行)。
有各种修复方法,您可以使用:-
dicionario = new Dicionario(str.get(0 + (count * 4)), str.get(1 + (count * 4)), str.get(2 + (count * 4)), str.get(3 + (count * 4)));
- str 最后会包含 4 * 行元素
如果在将值分配给 str 之前可以使用 str.clear();
之后不想使用 str,例如:-
//here I take the data from columns separately
str.clear();
str.add(cursor.getString(0));
str.add(cursor.getString(1));
str.add(cursor.getString(2));
str.add(cursor.getString(3));
- 在这种情况下,str 中只会有 4 个元素
或者您可以取消 str 并使用 :-
直接应用当前行的值public ArrayList<Dicionario> retornaArray() {
controlaBanco = new controlaBanco(this);//the classe with methods to manipule the data in a database
//List<String> str = new ArrayList<>(); //<<< not needed
cursor = controlaBanco.carregaDados(); //this will give me the cursor with some data from a database(sqlite)
arr = new ArrayList<Dicionario>(); //an arraylist formated to hand my data and then put it in a personalized listview
int count = 0; //an number to control the data flow(it's 3000 linea of data and I guess my application might get slow by this
while (!cursor.isAfterLast() && count <= 1000) {
//here I take that data to formating this in my class
dicionario = new Dicionario(
cursor.getString(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3)
);
//here I put my formated data into my formated array
arr.add(dicionario);
count++; //the count is upping...
}
//now I receive the array and returning this to my personalized adapter
return arr;
}
我个人会使用:-
public ArrayList<Dicionario> retornaArray() {
controlaBanco = new controlaBanco(this);//the classe with methods to manipule the data in a database
cursor = controlaBanco.carregaDados(); //this will give me the cursor with some data from a database(sqlite)
arr = new ArrayList<Dicionario>(); //an arraylist formated to hand my data and then put it in a personalized listview
while (cursor.moveToNext() && cursor.getPosition() < 1000) {
//here I take that data to formating this in my class
arr.add(new Dicionario(
cursor.getString(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3)
));
}
//now I receive the array and returning this to my personalized adapter
return arr;
}
关于java - 从外部循环中获取值在内部循环中接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50281537/