【发布时间】:2014-01-30 13:59:06
【问题描述】:
所以请多多包涵……我正在尝试从我的数据库表中获取所有信息,并将该信息输出到一个不错的自定义 ListView(我已经构建)中。
MySQLiteHelper.java(我用来抓取信息的工具)
...
public List<String> getAllLogs() {
List<String> List = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + TABLE_GASLOG;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()){
do {
List.add(cursor.getString(1));
} while (cursor.moveToNext());
}
return List;
}
...
gasLog.java(我用来获取/设置我的所有信息)
...
public class gasLog {
private int id;
private double pricePerGallon;
private double gallons;
private double odometer;
private String date;
private String filledOrNot; //This will be a 0 or 1 value.
private String comments;
public gasLog(){}
public gasLog(double pricePerGallon, double gallons, double odometer, String date, String filledOrNot, String comments){
super();
this.id = id;
this.pricePerGallon = pricePerGallon;
this.gallons = gallons;
this.odometer = odometer;
this.date = date;
this.filledOrNot = filledOrNot;
this.comments = comments;
}
//getters & setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getPricePerGallon() {
return pricePerGallon;
}
public void setPricePerGallon(double pricePerGallon) {
this.pricePerGallon = pricePerGallon;
}
public double getGallons(){
return gallons;
}
public void setGallons(double gallons){
this.gallons = gallons;
}
public double getOdometer(){
return odometer;
}
public void setOdometer(double odometer){
this.odometer = odometer;
}
public String getDate(){
return date;
}
public void setDate(String date){
this.date = date;
}
public String getFilledOrNot(){
return filledOrNot;
}
public void setFilledOrNot(String filledOrNot){
this.filledOrNot = filledOrNot;
}
public String getComments(){
return comments;
}
public void setComments(String comments){
this.comments = comments;
}
public String toString() {
return "Date: " + date + ", Price: " + pricePerGallon + ", " + gallons + " Gallons, " +
", Odometer Reading: " + odometer +
", Full fill: " + filledOrNot;
}
}
history.java(我在其中扩充视图并将所有信息调用到)。
...
public class history extends ListActivity {
// Log table name
private static final String TABLE_GASLOG = "gasLog";
// Log table columns names
private static final String KEY_ID = "id";
private static final String KEY_PRICE_PER_GALLON = "pricePerGallon";
private static final String KEY_GALLONS = "gallons";
private static final String KEY_ODOMETER = "odometer";
private static final String KEY_DATE = "date";
private static final String KEY_FILLED_OR_NOT = "filledOrNot";
private static final String KEY_COMMENTS = "comments";
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);
List<gasLog> list = new ArrayList<gasLog>();
list = db.getAllLogs();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listContent.setAdapter(adapter);
}
}
所以我知道我在 history.java 的底部需要更多类似于..的东西。
// THE DESIRED COLUMNS TO BE BOUND
String[] columns = new String[] { People.NAME, People.NUMBER };
// THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
int[] to = new int[] { R.id.name_entry, R.id.number_entry };
但我不太确定一切应该是什么。我曾尝试使用 getContentResolver,但我不确定如何设置 URI(或获取我的数据库的 URI),或者这是否是正确的方法。
我的 history.xml 包含一个列表视图,我有一个 bg.xml 文件,其中包含列表视图中每条记录的布局。现在我只能让它返回gasLog.java底部看起来凌乱的String toString()
任何帮助将不胜感激,如果有人能给我一些指导,也许为什么?期待在这方面学习一些我可以应用的东西。太感谢了!对不起,我是个新手!
编辑:
只是想确保我清楚这部分。 在 GasCursorAdapter.java 我将像这样设置 bindView:
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);
}
并且将为每个视图/数据库字段执行此操作(然后将值分配给视图)(我认为?!)
就这部分而言,我不太确定该放在哪里..
MySQLiteHelper db = new MySQLiteHelper(this);
Cursor cursor = db.getAllLogs();
GasCursorAdapter adapter = new GasCursorAdapter(content, cursor, 0);
setAdapter(adapter);
现在我在 history.java 中有它,但我得到 内容无法解析为变量
除此之外,我认为感谢您,我已经弄清楚了!!!
再次感谢您的帮助!
【问题讨论】:
-
您可能想使用
CursorAdapter,但那是另一回事了。
标签: android sql sqlite android-listview cursor