【发布时间】:2015-06-03 02:11:30
【问题描述】:
我知道如何使用自定义适配器管理列表视图。 我会从 SQLite 数据库中填充我的列表视图。 这是我的 CartHandler:
public class CartHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "cartManager";
// Contacts table name
private static final String TABLE_PRODUCTS = "products";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_NO = "number";
private static final String KEY_PIECES = "pieces";
private static final String KEY_PRICE = "price";
private static final String KEY_TOT_PRICE = "totprice";
public CartHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_NO + " TEXT," + KEY_PIECES + " TEXT,"+ KEY_PRICE + " TEXT," + KEY_TOT_PRICE + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
// Create tables again
onCreate(db);
}
// Adding new contact
void addProduct(CartRow product) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, product.getName()); // Contact Name
values.put(KEY_NO, product.getNumber()); // Contact Phone
values.put(KEY_PIECES, product.getPieces());
values.put(KEY_PRICE, product.getPrice());
values.put(KEY_TOT_PRICE, product.getTotPrice());
// Inserting Row
db.insert(TABLE_PRODUCTS, null, values);
db.close(); // Closing database connection
}
// Getting single contact
CartRow getProduct(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_PRODUCTS, new String[] { KEY_ID,
KEY_NAME, KEY_NO, KEY_PIECES, KEY_PRICE, KEY_TOT_PRICE }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
CartRow product = new CartRow(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), Integer.parseInt(cursor.getString(2)), Integer.parseInt(cursor.getString(3)), Integer.parseInt(cursor.getString(4)), Integer.parseInt(cursor.getString(5)));
// return contact
return product;
}
// Getting All Contacts
public List<CartRow> getAllProducts() {
List<CartRow> productList = new ArrayList<CartRow>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_PRODUCTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
CartRow product = new CartRow();
product.setID(Integer.parseInt(cursor.getString(0)));
product.setName(cursor.getString(1));
product.setNumber(Integer.parseInt(cursor.getString(2)));
product.setPieces(Integer.parseInt(cursor.getString(3)));
product.setPrice(Integer.parseInt(cursor.getString(4)));
product.setTotPrice(Integer.parseInt(cursor.getString(5)));
// Adding contact to list
productList.add(product);
} while (cursor.moveToNext());
}
// return contact list
return productList;
}
// Updating single contact
public int updateProduct(CartRow product) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, product.getName());
values.put(KEY_NO, product.getNumber());
values.put(KEY_PIECES, product.getPieces());
values.put(KEY_PRICE, product.getPrice());
values.put(KEY_TOT_PRICE, product.getTotPrice());
// updating row
return db.update(TABLE_PRODUCTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(product.getID()) });
}
// Deleting single contact
public void deleteProduct(CartRow product) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_PRODUCTS, KEY_ID + " = ?",
new String[] { String.valueOf(product.getID()) });
db.close();
}
// Getting contacts Count
public int getProductsCount() {
String countQuery = "SELECT * FROM " + TABLE_PRODUCTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}}
这是我的 CartRow 课程:
public class CartRow {
//private variables
int _id;
String _name;
int _number;
int _pieces;
int _price;
int _totprice;
// Empty constructor
public CartRow(){
}
// constructor
public CartRow(int id, String name, int number, int pieces, int price, int totprice){
this._id = id;
this._name = name;
this._number = number;
this._pieces = pieces;
this._price = price;
this._totprice = totprice;
}
// constructor
public CartRow(String name, int number, int pieces, int price, int totprice){
this._name = name;
this._number = number;
this._pieces = pieces;
this._price = price;
this._totprice = totprice;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
public String getName(){
return this._name;
}
public void setName(String name){
this._name = name;
}
public int getNumber(){
return this._number;
}
public void setNumber(int number){
this._number = number;
}
public int getPieces(){
return this._pieces;
}
public void setPieces(int pieces){
this._pieces = pieces;
}
public int getPrice(){
return this._price;
}
public void setPrice(int price){
this._price = price;
}
public int getTotPrice(){
return this._totprice;
}
public void setTotPrice(int totprice){
this._totprice = totprice;
}}
这是我想在列表视图中使用的布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:background="@android:color/white"
android:paddingBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/number"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@android:color/black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/name"
android:layout_alignBottom="@+id/number"
android:layout_toRightOf="@+id/number"
android:layout_toEndOf="@+id/number"
android:layout_marginLeft="25dp"
android:layout_marginStart="29dp"
android:textColor="@android:color/black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/pieces"
android:layout_below="@+id/name"
android:layout_alignLeft="@+id/name"
android:layout_alignStart="@+id/name"
android:layout_marginLeft="1dp"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/price"
android:layout_below="@+id/pieces"
android:layout_alignLeft="@+id/pieces"
android:layout_alignStart="@+id/pieces" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/totprice"
android:layout_alignTop="@+id/name"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#ffe20c18" />
</RelativeLayout>
我正确地填充了数据库并读取了 LogCat 中的内容。 不知道如何填充我的列表视图。试图查看类似的问题,但发现了如何填充单个信息。 我应该已经拥有让它工作所需的大部分代码,我不会更改整个代码来遵循示例教程。 谁能帮我? 任何建议将不胜感激。提前致谢。
【问题讨论】:
-
显示您的列表视图适配器代码
-
是的,显示您的自定义 listView 适配器。那么只有我们可以帮助你:)
-
我不知道在这种情况下如何设置我的适配器,这是我的问题。 Varun Madhvani 的回答应该有效,但我错过了中间的重点。
标签: android sqlite listview android-listview custom-adapter