需要你的帮助。我有类 ExternalDbOpenHelper,我在其中连接到我的 SQLite 数据库。连接成功。在我的活动中,我想选择 4 列的数据并在 ListView 中键入它。我在 fillFields() 方法中使用 SimpleCursorAdapter。但我在我的设备上只看到空白字段。但是设备上的字段数等于数据库中的行数。请告诉我我的错误在哪里?ListView中是否需要使用ArrayList或其他容器来输入数据?
public class IdevOwnSqliteDbActivity extends Activity {
private static final String DB_NAME = "SergBase.db";
final String LOG_TAG = "myLogs";
private SQLiteDatabase database;
private ListView listView;
private ArrayList<String> fields;
String sqlQuery = "select orders_products.products_id as _id,orders_products.products_name as Name,orders_products.final_price as FinalPrice, orders_status_history.date_added as Date"
+ " " +"from orders_products"
+ " "+"inner join orders_status_history"
+ " "+"where orders_products.orders_id=orders_status_history.orders_id";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView)findViewById(R.id.list);
ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
database = dbOpenHelper.openDataBase();
fillFields();
}
private void fillFields() {
//list = new ArrayList<DataContainer>();
Cursor fieldsCursor = database.rawQuery(sqlQuery, null);
Log.d(LOG_TAG, "Hello world!" );
Log.d(LOG_TAG, fieldsCursor.getColumnName(3) );
fieldsCursor.moveToFirst();
if(!fieldsCursor.isAfterLast()) {
do {
String name = fieldsCursor.getString(1);
fields.add(name);
} while (fieldsCursor.moveToNext());
}
String[] columns= new String[] {fieldsCursor.getColumnName(0), fieldsCursor.getColumnName(1), fieldsCursor.getColumnName(2),fieldsCursor.getColumnName(3)};
int[] to = new int[]{R.id.ProductID,R.id.ProductName,R.id.FinalPrice, R.id.Date};
startManagingCursor(fieldsCursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,
fieldsCursor,
columns,
to);
System.out.println(fieldsCursor.getColumnName(2));
listView.setAdapter(adapter);
}