English 中文(简体)
动态填充字符串数组
原标题:Populate a String Array dynamically

我正试图为我父母的企业制作一款应用程序,但此时我发现了一个问题。

我正试图从SQLite数据库中获取一些字符串,并将它们存储到一个数组中。

我正在尝试:

            //This opens the db
            SQLManager info = new SQLManager(this);
            info.open();              

            String[] data = {"", "", "", ""};

            for (int i = 1; i == 3; i++)
            {
                data[i]=(info.getProduct(i));
            }

            info.close();
            tv.setText(data[0]);
            tv.setText(data[1]);
            tv.setText(data[2]);

info.getproduct是一个从数据库中获取字符串的方法。这很好用。问题是我无法更新数组的值。它总是表现得一样。

知道吗?

问题回答

你这样做,

  String[] data = new String[4];

        for (int i = 0; i <data .length; i++)
        {
            data[i]=(info.getProduct(i));
        }

它对你有帮助。

你应该这样做

String[] data = {"", "", "", ""};

            for (int i = 0; i <data .length; i++)
            {
                data[i]=(info.getProduct(i));
            }

for循环的用法如下

for(initializatin;condition;increment)

而在你的情况下,它不是条件,它是一个声明,所以要努力。

对于(int i=1;i<;=3;i++),可以更改为

The second argument in the for loop is supposed to work as a while-condition, meaning the loop will run while it is true, as i starts as 1 it will not be 3 and thus the loop terminates at once. You probably want a loop that looks like this:

for( int i = 1; i<=3; i++ ){ //Are you sure you want to start from 1? The first element in an array has index 0.
    //Loop
}

此循环将永远不会运行,您应该将其更改为:

for(int i =0;i<3;i++){
}

不是i==3,而是使用i<;3在for循环中





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...