【发布时间】:2014-01-30 17:46:31
【问题描述】:
所以我试图从数据库中获取我的信息并将其返回到我已经创建的自定义格式的 ListView 中。
从我的 MainActivity.java 我正在启动我的 history.java 文件,该文件应该显示所有数据!
history.java如下所示
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
Typeface tf = Typeface.createFromAsset(getAssets(),"Roboto-Light.ttf");
ListView listContent = (ListView) findViewById(android.R.id.list);
TextView history = (TextView) findViewById(R.id.history);
history.setTypeface(tf);
MySQLiteHelper db = new MySQLiteHelper(this);
Cursor cursor = db.getAllLogs();
Log.d("history.java", "finished Cursor cursor = db.getAllLogs();");
GasCursorAdapter adapter = new GasCursorAdapter(this, cursor, 0);
Log.d("history.java", "GasCursorAdapter adapter = new GasCursorAdapter(this, cursor, 0);");
listContent.setAdapter(adapter);
Log.d("history.java", "setAdapter(adapter);");
cursor.close();
}
}
(在日志中我看到这里的一切都着火了——很好!)
这是我用来获取所有数据的 MySQLiteHelper.java。
public Cursor getAllLogs() {
List<String> List = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + TABLE_GASLOG;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
Log.d("MySQLiteHelper.java", "through getAllLogs()");
return cursor;
}
(我也看到了那里的日志 - 很好!)
这是我的 GasCursorAdapter.java
public class GasCursorAdapter extends CursorAdapter {
//private LayoutInflater mInflater = null;
public GasCursorAdapter(Context context, Cursor c, int flags){
super(context, c, flags);
//mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("inGasCursorAdapter", "GasCursorAdapter");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.card_background, parent, false);
Log.d("inGasCursorAdapter", "newView");
return retView;
//return mInflater.inflate(R.layout.card_background, parent, false);
}
@Override
public void bindView(View v, Context context, Cursor cursor){
TextView cardDate = (TextView) v.findViewById(R.id.cardDate);
int date = cursor.getColumnIndexOrThrow(MySQLiteHelper.KEY_DATE);
cardDate.setText(date);
TextView cardGallons = (TextView) v.findViewById(R.id.cardGallons);
cardGallons.setText((int) cursor.getDouble(cursor.getColumnIndex(cursor.getColumnName(3))));
Log.d("inGasCursorAdapter", "through bindView");
}
}
我看到它通过了第一种方法,但是 newView 和 bindView 方法永远不会触发。我想正因为如此,我的 history.java 页面只是空白,没有数据显示:(
关于我可能有什么问题或遗漏的任何想法?
提前致谢!
【问题讨论】:
-
您检查过您的
Cursor中有多少行吗?如果为空,则不会显示任何内容。 -
我为游标做了一个日志,我得到以下信息:01-30 12:51:29.533: D/cursor(21492): android.database.sqlite.SQLiteCursor@41f450d0 不确定是否是检查与否的正确方法?
-
不,
cursor.getCount()为您提供行数。 -
你也在打开你的数据库。你声明它但从不打电话给
db.open()。运行cursor.getCount()将显示为零,因为它没有写入数据库。
标签: java android android-cursoradapter