我正在尝试使用每列中的数据填充RecyclerView。
我想检索列下的所有值并动态地将其作为List返回。
最好的方法是什么?
附件是数据的示例图像:
我在应用程序中显示了一个表格列表,然后用户应该能够点击一个表格名称,并通过可滑动的标签查看每列下所有值的RecyclerView。
对于上图,用户应该看到CreateDate的回收者视图,然后滑动并查看CreatedByTablet的回收者视图等。
我遇到的问题是如何返回列中的所有值,动态。我对SQL并不熟悉,那么我将如何实现这一目标呢?该方法如何知道返回List<String>
或List<Integer>
?
这是我写的一些代码来尝试这个:
// Get all table names
public List<String> getAllTableNames() {
List<String> tableNames = new ArrayList<>();
try {
String query = "SELECT name FROM sqlite_master WHERE type='table'";
SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
Cursor cursor = db.rawQuery(query, null);
// Iterate
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
tableNames.add(cursor.getString(cursor.getColumnIndex("name")));
cursor.moveToNext();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return tableNames;
}
// Get all names of columns inside a table
public List<String> getAllColumnNames(String tableName) {
List<String> names = new ArrayList<>();
try {
String query = "SELECT * FROM " + tableName;
SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
Cursor cursor = db.rawQuery(query, null, null);
String[] columnNames = cursor.getColumnNames();
names = Arrays.asList(columnNames);
} catch (Exception e) {
e.printStackTrace();
}
return names;
}
public int getColumnType(String tableName, String columnName) {
String[] args = { columnName };
try {
int type;
String query = "SELECT * FROM " + tableName;
SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
Cursor cursor = db.rawQuery(query, args);
Log.d(TAG, "Column Index: " + String.valueOf(cursor.getColumnIndex(columnName)));
type = cursor.getType(cursor.getColumnIndex(columnName));
return type;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public List getAllRows(String tableName, String columnName, int type) {
Log.d(TAG, "Getting all rows");
switch (type) {
case 1:
List<Integer> integerList = createIntegerList(tableName, columnName);
return integerList;
case 3:
List<String> stringList = createStringList(tableName, columnName);
return stringList;
}
return null;
}
private List<Integer> createIntegerList(String tableName, String columnName) {
List<Integer> integerList = new ArrayList<>();
try {
String query = "SELECT * FROM " + tableName;
SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
int number = cursor.getInt(cursor.getColumnIndex(columnName));
integerList.add(number);
cursor.moveToNext();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return integerList;
}
private List<String> createStringList(String tableName, String columnName) {
List<String> integerList = new ArrayList<>();
try {
String query = "SELECT * FROM " + tableName;
SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
String name = cursor.getString(cursor.getColumnIndex(columnName));
integerList.add(name);
cursor.moveToNext();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return integerList;
}
如何在Sqlite android中的表内的列中返回所有值的整数或字符串列表(取决于列数据类型)?