2012-03-06 3 views
1

질문 제목이 적절한 지 아닌지 잘 모르겠지만 많이 검색 한 후에이 질문을하고 있습니다. 내 SQLite는 테이블에서CursorAdapter를 사용하는 방법?

, 나는 열이

1: _id 2: position 3: path 

position: the position of the gridView where the Image is to set 
path: the path of the SDCard having corresponding image 

어떻게 어떻게해야합니까에게 경로에서 이미지를 얻을이 후의 GridView

GridView grid = (GridView) findViewById(R.id.play_grid_view); 
DBAdapter adapter = new DBAdapter(this); //My costum adapter for databse operation 
adapter.open(); 
Cursor cusor = adapter.getAllImages(); //returns cursor with 3 columns mentioned above 
startManagingCursor(cusor); 

로 설정합니다 있나요?

+0

'2 : position'을 어떻게 사용 하시겠습니까? 그리드에서 이미지의 위치가 맞습니까? – hendrix

+0

격자가 4 * 4이고 위치가 5를 반환한다고 가정하면 그 이미지는 격자의 2 행 2 열에 표시되어야합니다. –

답변

1

position 열이 그리드의 이미지 위치를 의미하는 경우이 열을 사용하여 쿼리를 정렬하면 커서 어댑터가 DB에 설정된 위치에 따라 그리드를 채 웁니다.

gridview 셀을 건너 뛰는 경우 (데이터베이스에 다음 위치가 있다고 가정하면 1,2,4) -이 어댑터는 1,2,3을 채울 것입니다. 위치 확인)

public class ImageCursorAdapter extends CursorAdapter { 
    public ImageCursorAdapter(Context context, Cursor c) { 
     super(context, c); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     String pos = cursor.getString(positionColumnIndex); 
     String path = cursor.getString(pathColumnIndex); 
     ImageView image = (ImageView)view; 
     image.setImageDrawable(Drawable.createFromPath(path)); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     View v = new ImageView(context); 
     bindView(v, context, cursor); 
     return v; 
    } 
} 
+0

ayoutInflater의 사용법과 사용시기를 말할 수 있습니까? 이 예제를 사용하여 여러 가지 예제를 보았지만 왜 이것을 사용하는지 이해할 수 없습니다. –

+0

layout inflater는 XML 파일에서 View의 인스턴스를 만드는 데 사용됩니다. – hendrix

+0

그러나 ImageViews에 XML 파일이 없습니다. 내 XML 파일에서 방금 GriView를 선언하고 비트 맵을 설정하려고합니다. 그게 왜 내가 ImageView 이미지 대신 쓸 것을 혼란 스럽습니까 = (ImageView) view.findViewById (R.id.yourImageView); bindView()에서 –

1

SimpleCursorAdapter을 찾고 있습니다. 재정의하여 bindView() 메서드를 사용자 정의해야 원하는 것을 표시 할 수 있습니다.

CursorAdapter도 사용해 볼 수 있습니다. tutorial ..

+0

SimpleCursorAdapter 객체를 생성하기 위해 "from"및 "to"인수를 전달할 때 혼란 스럽습니다. –

관련 문제