我正在向数据库类发送一些String参数。我在那里调试,我正在看我的所有参数正确到那里。但是当我读取数据库时,一些列总是返回字符串零“0”。我工作三个小时,我找不到解决方案。我在这里分享我的代码。等你的建议谢谢你。
我的数据库类
@Override
public void onCreate(SQLiteDatabase db) {
String Create_Table = "CREATE TABLE " + YAYIN_TABLE + "("
+ GONDERI_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TOPLAM_FIYAT + " TEXT,"
+ GONDERI_ZAMAN + " TEXT,"
+ KOMBIN + " TEXT,"
+ LIKE + " TEXT)";
db.execSQL(Create_Table);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//implemented Method
}
public void YeniGonderi(String topFiyat,String gonderiZaman,String filename) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TOPLAM_FIYAT,topFiyat);
values.put(GONDERI_ZAMAN,gonderiZaman);
values.put(KOMBIN,filename);
db.insert(YAYIN_TABLE,null,values);
db.close();
}
public ArrayList<HashMap<String,String>> Yayinlar(){
SQLiteDatabase db = getReadableDatabase();
String SelectQuery = "SELECT * FROM " + YAYIN_TABLE;
Cursor cursor = db.rawQuery(SelectQuery,null);
ArrayList<HashMap<String,String>> yayinlar = new ArrayList<HashMap<String,String>>();
if (cursor.moveToFirst()) {
do {
HashMap<String,String> map = new HashMap<String,String>();
for (int i = 0;i<cursor.getColumnCount();i++) {
map.put(cursor.getColumnName(i),String.valueOf(cursor.getInt(i)));
}
yayinlar.add(map);
}while (cursor.moveToNext());
}
cursor.close();
db.close();
return yayinlar;
}
我的价值发送代码(Everythings就在这里。我正在调试并观察所有变量。)
DateFormat df = new SimpleDateFormat("MMM d, yyyy");
String now = df.format(new Date());
db = new DBase(getContext());
db.YeniGonderi(topfs,now,"TEST");
ListView getView方法
HashMap<String,String> map = arayList.get(position);
Gonderi gonderi = new Gonderi(map.get(GONDERI_ID),map.get(TOPLAM_FIYAT),map.get(LIKE),map.get(GONDERI_ZAMAN),map.get(KOMBIN));
GONDERI_ZAMAN和KOMBIN总是返回String Zero。但TOPLAM_FIYAT恢复正常。
答案 0 :(得分:2)
据我了解,您正在尝试将光标的每一行存储在List
中,哪些元素为HashMap
s将包含列的名称作为键和列value作为地图值。如果您确实尝试这样做,那么while
内的for
和Yayinlar()
循环是正确的。
问题是您将列声明为TEXT
,并且您尝试使用cursor.getInt(i)
获取这些列。尝试更改:
map.put(cursor.getColumnName(i),String.valueOf(cursor.getInt(i)));
有:
map.put(cursor.getColumnName(i), cursor.getString(i));
答案 1 :(得分:1)
我认为问题出在以下方面:
map.put(cursor.getColumnName(i),String.valueOf(cursor.getInt(i)));
你在循环中使用不必要的for循环。您没有指定列名来获取map的值。
试试这个:
if (c.moveToFirst()) {
do {
HashMap<String,String> map = new HashMap<String,String>();
map.put(cursor.getColumnName(i),c.getString(c.getColumnIndex("TOPLAM_FIYAT")));
yayinlar.add(map);
}while (c.moveToNext());
}