所以我试图从数据库中获取信息并将其返回到我已经创建的自定义格式化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页面只是空白,没有数据显示:(
关于我可能出现故障或遗失的任何想法?
提前致谢!
答案 0 :(得分:0)
不是将包含游标的数据传递到GasCursorAdapter
构造函数,而是在将其附加到swapCursor
之后尝试使用适配器的ListView
方法。无论如何,更好的方法是使用CursorLoader
类,因为它提供了从UI线程外部的数据源加载。是的,您应该为此实现自己的ContentProvider
,但是您可以扩展AsyncTaskLoader
类并使用MySQLiteHelper
直接检索数据