SQLite的到ListView控件(androidhive的版本) [英] SQLite to ListView(androidhive's version)

查看:148
本文介绍了SQLite的到ListView控件(androidhive的版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从SQLite的数据插入到ListView。我曾尝试看看例如:codeS,但他们不工作。该数据库的部分是从AndroidHive的SQL教程。在他的岗位在code,以获得数据库的所有数据(我编辑的数据字​​段):

I am trying to insert data from SQLite to a listview. I have tried look for example codes but they do not work. The database part is from AndroidHive's SQL tutorial. In his post the code to get all data in DB is(i edited the data fields) :

// Getting All carts item
public List<Cart> getAllCart() {
    List<Cart> cartList = new ArrayList<Cart>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Cart cart = new Cart();
            cart.setID(Integer.parseInt(cursor.getString(0)));
            cart.setPID(Integer.parseInt(cursor.getString(1)));
            cart.setName(cursor.getString(2));
            cart.setPrice(Double.parseDouble(cursor.getString(3)));
            cart.setQut(Integer.parseInt(cursor.getString(4)));
            // Adding cart to list
            cartList.add(cart);
        } while (cursor.moveToNext());
    }

    // return cart list
    return cartList;
}

怎样把这个getAllCart()到我的列表视图。非常感谢!

how do put this "getAllCart()" to my listview. Thanks alot!!

推荐答案

得到SQLite的数据你可以尝试这样的:

to get data from SQLite you can try like this:

      public List<String[]> selectAll()
{

    List<String[]> list = new ArrayList<String[]>();
    Cursor cursor = db.query(TABLE_NAME, new String[] { "id","name","number","skypeId","address" },
            null, null, null, null, "name asc"); 

    int x=0;
    if (cursor.moveToFirst()) {
        do {
            String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4)};

            list.add(b1);

            x=x+1;
        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    } 
    cursor.close();

    return list;
}

和显示它在列表视图:

    List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 =null ;
     names2 = dm.selectAll();

    stg1=new String[names2.size()]; 

    int x=0;
    String stg;

    for (String[] name : names2) {
        stg = name[1]+" - "+name[2]+ " - "+name[3]+" - "+name[4];

        stg1[x]=stg;
        x++;
    }


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(   
            this,android.R.layout.simple_list_item_1,   
            stg1);
    this.setListAdapter(adapter);
}      

参照本教程,你会得到一个想法: http://www.vogella.com /articles/AndroidSQLite/article.html

这篇关于SQLite的到ListView控件(androidhive的版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆