2014-01-30 5 views
0

내 데이터베이스의 내용을 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

+0

삽입 방법도 알려주세요. 선택 쿼리는 정상이므로 실제로 삽입하는 데이터 여야합니다. – Darwind

+0

@Darwind가 추가되었습니다. – Rob

+0

GasCursorAdapter에 대한 코드를 게시 할 수 있습니까? –

답변

0

테이블에 _id 열이 있습니까? 그렇지 않은 경우 개별 열을 선택해야 할 수도 있습니다 (* 선택 대신 수행해야 함). 키 기본 키를 별칭으로 _id로 지정하십시오.

+0

예 테이블에 _id – Rob

+0

이라는 열이 있습니다. 왜'* '를 사용하는 대신 모든 열의 이름을 지정 하시겠습니까? – Darwind

+0

성능/유지 관리 일입니다. "select *"는 모든 열을 가져 오므로 필요하지 않은 다른 열의 데이터를 가져 와서 버릴 수 있습니다. 테이블에 오늘 여분의 열이 없더라도 나중에 유지 관리 문제가 될 수 있습니다. –

관련 문제