来自DB的Listview

时间:2015-06-25 06:05:23

标签: android sqlite listview

我试图从数据库中获取一些数据并显示在列表视图中,但是他们logcat说明在从数据库访问数据之前确保Cursor正确初始化。这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cotizacionescolumna);
    lvCotizacion = (ListView)findViewById(R.id.lista_cotizaciones);
    list_cotizaciones = new ArrayList<String>();
    llenarLista();
    eventos();
}

public void llenarLista(){

        CotiCadSqlite db = new CotiCadSqlite(CotizacionesColumnnaActivity.this);
        Cursor c = db.obtenerNombresColumna();

        if(c.moveToFirst()){
            do{
                columna = c.getString(1);
                list_cotizaciones.add(columna);
            }while (c.moveToNext());
        }
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_cotizaciones);
        lvCotizacion.setAdapter(adapter);
}

2 个答案:

答案 0 :(得分:0)

 columna = c.getString(1);

c.getString(c.getColumnIndex(1))

以及

if (null != c) {
            if (c.moveToFirst()) {
                do {
                    //your code goes here


                } while (c.moveToNext());
            }
            c.close();

答案 1 :(得分:0)

尝试这样,

      Cursor c = helper.DatabaseMethod();// retrive data
                    c.moveToFirst();
                          while (c.isAfterLast() == false) 
         {

         String   dataSaved = c.getString(c.getColumnIndex(DataBaseHelper.KEY_EMAIL));//your column index name       
   list_cotizaciones.add(dataSaved );

                                   c.moveToNext();
                                  }
                                  c.close();

     adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_cotizaciones);
               lvCotizacion.setAdapter(adapter);