while(cursor.moveToNext()){ int a1= cursor.getInt(cursor.getColumnIndex("d_head")); descA[i] = a1; String a2 = cursor.getString(cursor.getColumnIndex("d_name")); descB[i] = a2; String a3 = cursor.getString(cursor.getColumnIndex("d_post")); descC[i] = a3; String a4 = cursor.getString(cursor.getColumnIndex("d_type")); descD[i] = a4; String a5 = cursor.getString(cursor.getColumnIndex("d_content")); descE[i] = a5; i++; } h1.setBackgroundResource(descA[num]); //设置控件属性的代码 h2.setText(descB[num]); h3.setText(descC[num]); h4.setText(descD[num]); h5.setText("\u3000\u3000"+descE[num]);//加空格
时间: 2024-02-14 21:35:16 浏览: 29
这段代码主要是从数据库中读取数据,并将读取到的数据保存到`descA`、`descB`、`descC`、`descD`和`descE`等数组中。具体来说,它使用了`Cursor`对象来遍历数据库中的数据,然后将每行数据分别保存到相应的数组中,最后根据数组中指定的数据来设置UI控件的属性。其中:
- `cursor.moveToNext()` 判断是否还有下一行数据,如果有则返回true并将`Cursor`对象移动到下一行,否则返回false跳出循环。
- `cursor.getColumnIndex("d_head")` 根据列名获取该列在当前查询结果中的索引。
- `cursor.getInt()`、`cursor.getString()`等方法根据列索引获取该列的值,并将其转化为相应的数据类型。
- `descA[i] = a1`、`descB[i] = a2`等语句将读取到的数据分别保存到对应的数组中。
- `h1.setBackgroundResource(descA[num])`、`h2.setText(descB[num])`等语句根据数组中指定的数据来设置UI控件的属性,比如设置控件的背景、文本等。其中`num`是一个变量,表示当前需要显示的数据在数组中的下标。
相关问题
while(cursor.moveToNext()){ int a1= cursor.getInt(cursor.getColumnIndex("d_head")); descA[i] = a1; String a2 = cursor.getString(cursor.getColumnIndex("d_name")); descB[i] = a2; String a3 = cursor.getString(cursor.getColumnIndex("d_post")); descC[i] = a3; String a4 = cursor.getString(cursor.getColumnIndex("d_type")); descD[i] = a4; String a5 = cursor.getString(cursor.getColumnIndex("d_content")); descE[i] = a5; i++; }
这是一段用于从数据库中查询数据的代码。具体来说,通过使用cursor对象,执行查询操作,并通过遍历查询结果集,读取每一条记录中的数据,以便存储到相应的数组中。
在这里,通过使用cursor对象查询数据库中的记录,并通过遍历查询结果集,逐条读取记录中的数据,并将其存储到相应的数组中。其中,getInt()方法用于获取一条记录中的某个整数类型的数据,getString()方法用于获取一条记录中的某个字符串类型的数据,getColumnIndex()方法用于获取指定列名的列索引,以便从一条记录中读取相应的数据。
需要注意的是,在使用cursor对象查询数据库时,需要先调用其query()方法,指定要查询的表名、列名、查询条件等信息,以便获取查询结果集。另外,在遍历查询结果集时,需要先调用cursor.moveToFirst()方法,将游标移动到第一条记录的位置,然后再通过循环遍历的方式,逐条读取查询结果集中的记录。遍历完查询结果集后,需要调用cursor.close()方法,释放游标资源。
while(cursor.moveToNext()) { int a2 = cursor.getInt(cursor.getColumnIndex("_id")); desc[p] = a2; p++; }
这是一段用于遍历查询结果集的代码。具体来说,代码中通过调用Cursor对象的moveToNext()方法,将光标移动到查询结果集的下一行,并返回一个布尔值,表示是否移动成功。如果移动成功,则通过调用Cursor对象的getColumnIndex()方法获取"_id"列在结果集中的索引,并通过调用getInt()方法获取该列的值,并将其赋值给变量a2。接着将a2的值存储到数组desc中,并将p的值加1,以便继续存储下一个查询结果的值。
需要注意的是,在这里我们只查询了"_id"列,并将其存储到了一个数组中。如果需要查询其他列,可以通过调用Cursor对象的getColumnIndex()方法获取该列在结果集中的索引,并调用对应的get方法获取该列的值。另外,在使用Cursor对象进行查询时,需要注意在使用完毕后将其关闭,以释放资源。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)