【问题标题】:Issue with array being generated via looped query results通过循环查询结果生成数组的问题
【发布时间】:2012-03-14 02:53:49
【问题描述】:

我一直在寻找/对以下代码进行不同的处理:

public String[] getData() {

String[] columns = new String[]{ app_name, app_location, app_dateTime};
Cursor c = myDatabase.query(app_table, columns, null, null, null, null, null);

int iName = c.getColumnIndex(app_name);
int iLoc = c.getColumnIndex(app_location);
int iDate = c.getColumnIndex(app_dateTime);


String[] appointments = new String[c.getColumnCount()];
int j = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
    appointments[j] = " Appointment Name: " + c.getString(iName) + " \n Location: " + c.getString(iLoc) + " \n Date: " + c.getString(iDate) + " \n\n ";
     j++;
    }
return appointments;

    }
}

基本上,这段代码的目的是将我的查询结果输入一个数组,然后通过代码的“返回约会”部分在另一个类的 ListView 中访问。出于某种原因,我的代码似乎无法正常工作,我无法完全确定它,我尝试了循环中的循环我尝试过使代码变暗,然后扩展我可以开始工作的内容,但没有一个给出给我一个解决方案。

【问题讨论】:

    标签: android arrays sqlite loops cursor


    【解决方案1】:

    我认为不是

    String[] appointments = new String[c.getColumnCount()];
    

    你需要使用

    String[] appointments = new String[c.getCount()];
    

    getColumnCount() 返回列数(一直是 3 -name, location, dateTime-),你需要那里的行数。

    【讨论】:

    • 抱歉,我没有提到我也尝试过更改 String[] 约会 = new String[c.getCount()];到:字符串[]约会=新字符串[99];为了反复试验。我相信错误是在 for 循环本身的某个地方,但我找不到它。
    【解决方案2】:

    我很抱歉并感谢提交答案的人,我已经弄清楚问题出在哪里,它在我的另一个 java 类中。上面列出的循环功能齐全,所以如果有人需要找到一段代码来完成上面的要求,那就别再看了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 2015-09-22
      相关资源
      最近更新 更多