使用SimpleCursorAdapter清空ListView

时间:2012-07-09 15:26:50

标签: android listview simplecursoradapter

需要你的帮助。我有类ExternalDbOpenHelper,我连接到我的SQLite数据库。连接成功通过。在我的Activity中,我想选择4列的数据并在ListView中键入它。我在fillFields()方法中使用SimpleCursorAdapter。但我看到我的设备上只有空字段。但是设备上的字段数等于数据库中的行数。请告诉我哪里错了?是否有必要使用ArrayList或其他容器在ListView中键入数据?

    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);

}

0 个答案:

没有答案