我的应用程序从sqlite检索数据,但我看不到可用于突出显示数据的页面上的任何数据。
这是适配器。
private ShoppingDatabaseHandler sdb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping);
sdb = new ShoppingDatabaseHandler(this) ;
sdb.addShopping(new Shops(" ABC MARKET ", " 27872514 ", " 6 "));
sdb.addShopping(new Shops(" Fortress ", " 27454547 ", " 7 "));
List<Shops> values = sdb.getAllShops();
ArrayAdapter<Shops> adapter = new ArrayAdapter<Shops>(this, android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
这是布局。
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
我不知道是什么问题?
非常感谢。
这是ShoppingDatabaseHandler。
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_OWNER_TABLE = "CREATE TABLE " + TABLE_SHOP + "(" + KEY_NAME + " TEXT NOT NULL," + KEY_NUMBER + " TEXT NOT NULL," + KEY_LOCATION + " TEXT NOT NULL );";
db.execSQL(CREATE_OWNER_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_SHOP);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addShopping(Shops shops) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, shops.getName()); // Contact Name
values.put(KEY_NUMBER, shops.getNumber()); // Contact Phone
values.put(KEY_LOCATION, shops.getLocation());
// Inserting Row
db.insert(TABLE_SHOP, null, values);
db.close(); // Closing database connection
}
public List<Shops> getAllShops() {
List<Shops> shopdata = new ArrayList<Shops>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_SHOP;
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Shops shops = cursorToShops(cursor);
shopdata.add(shops);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return shopdata;
}
private Shops cursorToShops(Cursor cursor) {
Shops shops = new Shops();
shops.setName(cursor.getString(0));
shops.setNumber(cursor.getString(1));
shops.setLocation(cursor.getString(2));
return shops;
}
我是否需要在Cursor函数中添加toString()?
感谢您的帮助。
答案 0 :(得分:0)
然而,TextView被引用,它将被填充 数组中每个对象的toString()。您可以添加列表或数组 自定义对象。覆盖对象的toString()方法 确定将为列表中的项目显示的文本。
这意味着,如果您有空行,则 Shops 对象没有toString()方法,或者此方法返回空字符串。
答案 1 :(得分:0)
Android不知道如何显示“商店”项目,因为它是自定义类。 列表视图使用object.toString()来显示对象,因此您需要向Shops添加toString()方法,告诉Android如何显示它。
例如(根据品味调整),将其添加到Shops.java
中@Override
public String toString(){
return getName() + " (" + getNumber() + " ) located at: " + getLocation();
}