所以请耐心等待我...我正在尝试从我的数据库表中获取所有信息并将该信息输出到一个漂亮的自定义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包含一个listview,我有一个bg.xml文件,其中包含listview中每条记录的布局。现在我只能让它在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 中有这个,但我得内容无法解析为变量
除此之外我想想我想知道你了!
再次感谢您的帮助!
答案 0 :(得分:2)
我建议您使用CursorAdapter,它将跳过一些步骤并运行得更顺畅。我正在做类似于你的事情,并遇到很多性能问题,但我切换到了cursorAdapter,因此我的代码更容易理解并且更快。实现这一目标有几个部分,我将在下面向您展示。第一步是简单地返回光标,而不是尝试在数据库调用中处理它。
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);
return cursor;
}
这实际上会以您可以更好地利用它的方式传递查询。我管理我的程序的方式是从这里做这样的事情。
此处GasCursorAdapter
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);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.gasLayout,parent,false);
}
@Override
public void bindView(View v,Context context, Cursor cursor) {
//View is the view created by newView, take it and find your views and populate it, given the Cursor
}
}
创建它看起来像:
Cursor cursor=getAllLogs();
GasCursorAdapter adapter=new GasCursorAdapter(context, cursor,0);
setAdapter(adapter);