2011-04-05 10 views
2

제목으로 데이터베이스에서 내 데이터를 목록보기로 연결하는 데 문제가 있습니다. 문제는 어댑터가 반환되는 마지막 행만 설정한다는 것입니다. 데이터베이스 테이블의 행.안드로이드 : setListAdapter는 데이터베이스의 마지막 행만 설정합니다.

내가 달성하기 위해 노력하고 무엇의 예 :

표 동물 : 원숭이 개 타이거

화면에로만 타이거 표시됩니다 고양이.

데이터베이스에서 커서를 돌려 내 방법 : 목록보기

dbm = new MyDBManager(this); 
    dbm.open(); 
    dbm.deleteAnimals(); 
    dbm.populateDB(); 

    // after populating, set the adapter.. 
    myCursor = dbm.getAllAnimals(); 

String[] columns = new String[] {"animal_name"}; 
int[] to = new int[] {R.id.animal}; 

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.row, myCursor, columns, to); 
    this.setListAdapter(mAdapter); 

public Cursor retrieveAnimals(){ 

    return = DB.query(ANIMAL_TABLE, new String[] { 
      KEY_ROWID, 
      KEY_ANIMALNAME, 
      }, 
      null, 
      null, 
      null, 
      null, 
      null);  
} 

및 설정 I 예감 내 문제가 자동으로 이동된다는 점에서, 커서 위치에 자리 잡고 있습니다 마지막 행 어댑터를 설정할 때, 나는 내 문제에 대한 해결책을 찾으려고 노력했지만 행운이 없었다.

미리 제안 해 주셔서 감사합니다.

편집 : 방법이 retrieveAnimals()에서 DB

public long populateDB(){ 


    ContentValues initialValues = new ContentValues(); 

    for(int i = 0; i < animalName.length; i++){ 
     initialValues.put(KEY_ANIMALNAME, animalName[i]); 
    } 

    return DB.insert(ANIMAL_TABLE, null, initialValues); 

} 
+0

'myCursor.getCount()'는 무엇을 반환합니까? – rajath

답변

1

을 채우는 선은 return DB.query(...)이 (returnDB.query 사이의 =의 부족주의)이어야한다.

그 외에도 코드가 정상적으로 보입니다. 메모리 누수를 피하려면 커서에 startManagingCursor으로 전화해야합니다 (getAllAnimals에 전화 한 후).

편집 :로 채우기 방법을 변경 :

또는
public long[] populateDB(){ 


    ContentValues initialValues = new ContentValues(); 
    long[] rowIds = new long[animalName.length]; 

    for(int i = 0; i < animalName.length; i++){ 
     // here we are using a single ContentValues object and inserting it 
     // into the DB before changing the ContentValues and inserting it again 
     initialValues.put(KEY_ANIMALNAME, animalName[i]); 
     rowIds[i] = DB.insert(ANIMAL_TABLE, null, initialValues); 
    } 

    return rowIds; 

} 

(더 많은 메모리를 사용하지만, 유용, 아마도 당신이 그들을 삽입하기 전에 기록의 일부를 수정해야하는 경우) :

public void populateDB(){ 

    ContentValues[] initialValues = new ContentValues[animalName.length]; 

    for(int i = 0; i < animalName.length; i++){ 
     // create a new ContentValues for each object 
     initialValues[i] = new ContentValues(); 
     initialValues[i].put(KEY_ANIMALNAME, animalName[i]); 
    } 

    // now our contentvalues is filled we can insert them all 
    for(int i = 0; i < animalName.length; i++){ 
     DB.insert(ANIMAL_TABLE, null, initialValues[i]); 
    } 
} 
+0

답장을 보내 주신 Joseph에게 감사드립니다. =는 커서를 할당하고 위의 방법과 반대로 반환합니다. 제거시 아무 것도 변경되지 않았습니다. – Jnanathan

+0

이 경우, @ Rajath의 코멘트에 따라 - myCursor.getCount()가주는 것은 무엇입니까? 대답이 1이면 데이터베이스는 데이터베이스에서 1 레코드 만 반환합니다. –

+0

getCount()는 1을 반환합니다. 데이터베이스를 채우기 위해 내 메서드로 원래 게시물을 편집했습니다. 문제가있는 것으로 보일 수 없습니다. 기본적으로 커서는 있어야 할 때 커서가 1 행만 반환됩니다. 수개. – Jnanathan

1

했나 당신은 시도해 본다

myCursor.moveToFirst() 
+0

안녕하세요 Rajath 제안을 주셔서 감사합니다, 나는 DB에서 커서를 검색 후 바로이 코드를 추가했습니다, 그것은 차이를 만들지 않았습니다. – Jnanathan

관련 문제