내 데이터베이스의 내용을 ListView
에 표시하려고합니다. 뭔가 잘못된 것이 있습니다 (분명히) 그리고 데이터베이스에서 행 # 1을 여러 번 반환합니다.행 1을 여러 번 반환하는 커서
예를 들어 2 행이 있으면 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);");
과 내가 추측하고있어
...
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()
수행해야 뭔가 내 MySQLiteHelper.java입니다 ... 그러나 나는 그것을 정확하게 지적 할 수 없다. 내가 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
이
삽입 방법도 알려주세요. 선택 쿼리는 정상이므로 실제로 삽입하는 데이터 여야합니다. – Darwind
@Darwind가 추가되었습니다. – Rob
GasCursorAdapter에 대한 코드를 게시 할 수 있습니까? –