数组方法返回数组,只有第一个值?

发布于 2024-12-16 22:21:58 字数 954 浏览 0 评论 0原文

我正在尝试从 sqlLite DB 获取列数据并将该数据放入数组方法中,然后调用该方法并显示数据,由于某种原因我只获取列中的第一个变量,请帮忙!

-是的,该列中有更多变量

    public String[] getScore01() {

    String[] column1 = new String[] { KEY_NAME };
    Cursor c = ourDatabase.query(DATABASE_TABLE, column1, null, null, null,
            null, null);
    String[] result = {};

    int iName = c.getColumnIndex(KEY_NAME);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

        String[] result1 = { "" + c.getString(iName) };

        result = result1.clone();
    }
    return result;
}

,在这里我们调用该方法并将数据设置为数组...

        Database info = new Database(this);

    info.open();
    String[] data = info.getScore01().clone();
    info.close();

将文本视图 tvVVTEST 设置为 data[0] 有效,但将其设置为 data[1] 或其他任何内容都会失败为什么???

        tvVVTEST.setText("" + data[0]); //works
      //tvVVTEST.setText("" + data[1]); // Does Not Work

I am trying to get column data from sqlLite DB and put that data into an array method and then call the method and display the data, for some reason I am only getting the first variable in my column, help please!!!!

-Yes there are more variables in that column

    public String[] getScore01() {

    String[] column1 = new String[] { KEY_NAME };
    Cursor c = ourDatabase.query(DATABASE_TABLE, column1, null, null, null,
            null, null);
    String[] result = {};

    int iName = c.getColumnIndex(KEY_NAME);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

        String[] result1 = { "" + c.getString(iName) };

        result = result1.clone();
    }
    return result;
}

and here we call the method and get the data set it to an array...

        Database info = new Database(this);

    info.open();
    String[] data = info.getScore01().clone();
    info.close();

setting the textview tvVVTEST to data[0] works, but setting it to data[1] or anything else fails why???

        tvVVTEST.setText("" + data[0]); //works
      //tvVVTEST.setText("" + data[1]); // Does Not Work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苏辞 2024-12-23 22:21:58

看一下这个循环:

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

    String[] result1 = { "" + c.getString(iName) };

    result = result1.clone();
}
return result;

您只将 result1 设置为单个元素数组,然后将 result 设置为该数组的克隆。因此 result 将仅引用单元素数组。我建议您使用 List 代替:(

List<String> results = new ArrayList<String>();
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
    results.add(c.getString(iName));
}
return results;

更改您的方法以声明它也返回 List 。)

然后您应该能够使用

List<String> data = info.getScore01();
tvVVTest.setText(data.get(1)); // Assuming there *is* a second element

顺便说一句,您似乎在任何需要字符串的地方都使用 "" + x 。即使 x 不是 字符串,这也是一个坏主意 - 使用 String.valueOf(x) 代替 - 但当 x 不是字符串时,情况会更糟>x 一个字符串。

您也不需要到处克隆数组(或列表)。哦,您应该将 info.close() 方法放在finally 块中,这样即使抛出异常也可以关闭数据库。

Look at this loop:

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

    String[] result1 = { "" + c.getString(iName) };

    result = result1.clone();
}
return result;

You're only ever setting result1 to a single element array, and then result to a clone of that array. Therefore result will only ever refer to a single-element array. I suggest you use a List<String> instead:

List<String> results = new ArrayList<String>();
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
    results.add(c.getString(iName));
}
return results;

(Change your method to declare that it returns a List<String> as well.)

You should then be able to use

List<String> data = info.getScore01();
tvVVTest.setText(data.get(1)); // Assuming there *is* a second element

By the way, you seem to be using "" + x wherever you want a string. That's a bad idea even when x isn't a string - use String.valueOf(x) instead - but it's even worse when x is a string.

You also don't need to be cloning arrays (or lists) all over the place. Oh, and you should put the info.close() method in a finally block, so that you close the database even if an exception is thrown.

阪姬 2024-12-23 22:21:58
info.open();
String[] data = info.getScore01().clone();
info.close();

没必要这样打电话。将其更改为

info.open();
String[] data = info.getScore01();
info.close();

并且您已声明结果两次。第二,确保数组值每次都会插入到新位置。否则它将覆盖,您将只得到一个值

已编辑
内部循环更改为

where you declare String [] result, Change to String []result=new String[c.getCount()];
 int i=0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

    result1[i] = c.getString(iName);
    i++;
}
return result;
info.open();
String[] data = info.getScore01().clone();
info.close();

No need to call like this. Change this to

info.open();
String[] data = info.getScore01();
info.close();

And you have declared result two times.Second make sure that array value will insert on new position everytime.Otherwise it will over write and you will get only one value

Edited
changes inside loop to

where you declare String [] result, Change to String []result=new String[c.getCount()];
 int i=0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

    result1[i] = c.getString(iName);
    i++;
}
return result;
坚持沉默 2024-12-23 22:21:58

添加以下内容:

System.out.println(data.length);

如果是一个,那么您的数组中可能只有一个元素。

Add the following:

System.out.println(data.length);

If its one then you probably only have one element in your array.

尹雨沫 2024-12-23 22:21:58

如果你可以使用 for() 语法,这对我来说是新闻。如果使用 while 循环会发生什么?

while( c.moveToNext() ) { 

if you can use that for() syntax that news to me. What happens if you use a while loop instead?

while( c.moveToNext() ) { 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文