0

我正在尝试将数据从 SQLite 插入列表视图。我已经尝试查找示例代码,但它们不起作用。数据库部分来自 AndroidHive 的 SQL 教程。在他的帖子中,获取数据库中所有数据的代码是(我编辑了数据字段):

// 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()”放到我的列表视图中。非常感谢!!

4

2 回答 2

0

您需要在 Activity 或 ListActivity 中创建一个 Database 类实例才能getAllCart访问

public class TempActivity extends Activity {
DatabaseHandler db;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        db = new DatabaseHandler(TempActivity.this);

        // Reading all Carts
        List<Cart> Carts = db.getAllCart(); //<< get all Cart details here    
       //... your code here..  

并且为了在 ListView 中显示 Carts ArrayList 值,您需要创建一个自定义适配器来添加 ListView 行中的所有项目。

对于使用自定义适配器创建 ListView,您可以查看以下教程:

使用自定义 ArrayAdapter 自定义 Android ListView 项

Android 列表视图示例

于 2013-01-26T06:28:19.370 回答
0

要从 SQLite 获取数据,您可以这样尝试:

      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

于 2013-01-26T06:29:11.283 回答