我試圖讓我的數據庫的內容出現在ListView
中。有一些錯誤(很明顯),它多次返回數據庫中的第1行。光標多次返回第1行
例如,如果有2行,它將返回行#1 2次。如果我添加另一條記錄,它將返回行#1 3次。
這裏是我的history.java
...
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);");
,這裏是我的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);
return cursor;
}
我猜這件事情,我需要在getAllLogs()
做...但我不能指出它。我嘗試過使用cursor.moveToFirst()
和moveToNext
,但這似乎產生了相同的結果......似乎知道有多個記錄,但只是多次返回第一個!
任何幫助,非常感謝!
EDIT(附加信息) newRecord.java
public class newRecord extends Activity {
String fill;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_record);
Typeface tf = Typeface.createFromAsset(getAssets(),"Roboto-Light.ttf");
final MySQLiteHelper db = new MySQLiteHelper(this);
final EditText pricePerGallon = (EditText) findViewById(R.id.pricePerGallon);
final EditText gallons = (EditText) findViewById(R.id.gallons);
final EditText odometer = (EditText) findViewById(R.id.odometer);
final EditText today = (EditText) findViewById(R.id.date);
final EditText comments = (EditText) findViewById(R.id.comments);
final CheckBox notCompleteFill = (CheckBox) findViewById(R.id.completeFill);
TextView save = (TextView) findViewById(R.id.save);
TextView reset = (TextView) findViewById(R.id.reset);
pricePerGallon.setTypeface(tf);
gallons.setTypeface(tf);
odometer.setTypeface(tf);
today.setTypeface(tf);
comments.setTypeface(tf);
notCompleteFill.setTypeface(tf);
save.setTypeface(tf);
reset.setTypeface(tf);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
today.setText(sdf.format(new Date()));
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
String pricePerGallonString = pricePerGallon.getText().toString();
final double pricePerGallonInt = Double.parseDouble(pricePerGallonString);
String gallonsString = gallons.getText().toString();
final double gallonsInt = Double.parseDouble(gallonsString);
String odometerString = odometer.getText().toString();
final double odometerInt = Double.parseDouble(odometerString);
String todayString = today.getText().toString();
String notCompleteFillString = notCompleteFill.getText().toString();
String commentsString = comments.getText().toString();
db.addGasLog(new gasLog(pricePerGallonInt, gallonsInt, odometerInt, todayString, fill, commentsString));
Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_LONG).show();
Intent i = new Intent(newRecord.this, history.class);
startActivity(i);
}
});
MySQLiteHelper.java
public void addGasLog(gasLog gasLog){
// for logging
Log.d("addGasLog", gasLog.toString());
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_PRICE_PER_GALLON, gasLog.getPricePerGallon()); // get price
values.put(KEY_GALLONS, gasLog.getGallons()); // get gallons
values.put(KEY_ODOMETER, gasLog.getOdometer()); // get odometer
values.put(KEY_DATE, gasLog.getDate()); // get date
values.put(KEY_FILLED_OR_NOT, gasLog.getFilledOrNot()); // get filledOrNot
values.put(KEY_COMMENTS, gasLog.getComments()); // get comments
// 3. insert
db.insert(TABLE_GASLOG, //table
null, //nullColumnHack
values); // key/value -> keys = column names/ values =
// 4. Close
db.close();
}
EDIT2 GasCursorAdapter.java
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
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){
/*
* cardDate
* cardGallons
* cardPrice
* cardMPG
*/
TextView cardDate = (TextView) v.findViewById(R.id.cardDate);
int date = cursor.getColumnIndexOrThrow(MySQLiteHelper.KEY_DATE);
cardDate.setText(Integer.toString(date));
TextView cardGallons = (TextView) v.findViewById(R.id.cardGallons);
int gallons = cursor.getColumnIndexOrThrow(MySQLiteHelper.KEY_GALLONS);
cardGallons.setText(Integer.toString(gallons) + "gal");
TextView cardPrice = (TextView) v.findViewById(R.id.cardPrice);
int price = cursor.getColumnIndexOrThrow(MySQLiteHelper.KEY_PRICE_PER_GALLON);
cardPrice.setText("$" + Integer.toString(price));
TextView cardMPG = (TextView) v.findViewById(R.id.cardMPG);
int MPG = cursor.getColumnIndexOrThrow(MySQLiteHelper.KEY_ODOMETER);
cardMPG.setText(Integer.toString(MPG));
TextView cardDelete = (TextView) v.findViewById(R.id.cardDelete);
cardDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
}
});
}
}
向我們展示您的插入方法。你的選擇查詢是可以的,所以它必須是你實際插入的數據。 – Darwind
@Darwind補充。 – Rob
你能發佈GasCursorAdapter的代碼嗎? –