2013-05-07 1 views
1

나는 Cursor으로 Spinner을 채우는 방법에 대해 SO와 다른 웹 사이트에 대한 많은 답변을 발견했지만, 모두 그 대신 deprectated SimpleCursorAdapter(Context, int, String[], int[]) 생성자를 사용합니다. 아무도 API 레벨 11 이상으로이를 수행하는 방법을 설명하지 못합니다.API 레벨 11 이후에 Spinner에 커서를 채우는 방법은 무엇입니까?

API는 LoaderManager을 사용하라고 알려주지 만 사용 방법은 확실하지 않습니다.

+1

SimpleCursorAdapter를 사용하는 대신 사용자 고유의 CursorAdapter를 구현하는 것이 좋습니다. –

+0

Thanks @DctororDrive, 귀하의 의견은 좋은 것입니다. 마술처럼 대답으로 바꾸는 것을 고려해 볼 수 있습니다. 커스텀'CursorAdapter'를 만드는 방법을 간략하게 설명하는 시간이 필요하다면 저에게 적어도 25 포인트를 이길 수 있습니다. – SteeveDroz

답변

1

SimpleCursorAdapter를 사용하는 대신 사용자 고유의 CursorAdapter를 구현하는 것이 좋습니다.

CursorAdapter를 구현하는 것은 다른 어댑터를 구현하는 것보다 어렵지 않습니다. CursorAdapter는 BaseAdapter를 확장하며 getItem(), getItemId() 메서드는 이미 재정의되어 실제 값을 반환합니다. pre-Honeycomb을 지원하는 경우 지원 라이브러리 (android.support.v4.widget.CursorAdapter)의 CursorAdapter를 사용하는 것이 좋습니다. 11 세 이후 인 경우에만 android.widget.CursorAdapter를 사용하십시오. swapCursor (newCursor)를 호출 할 때 notifyDataSetChanged()를 호출 할 필요가 없다는 사실을 염두에 두십시오.

import android.widget.CursorAdapter; 

public final class CustomAdapter 
     extends CursorAdapter 
{ 

    public CustomAdapter(Context context) 
    { 
     super(context, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); 
    } 


    // here is where you bind the data for the view returned in newView() 
    @Override 
    public void bindView(View view, Context arg1, Cursor c) 
    { 

     //just get the data directly from the cursor to your Views. 

     final TextView address = (TextView) view 
       .findViewById(R.id.list_item_address); 
     final TextView title = (TextView) view 
       .findViewById(R.id.list_item_title); 

     final String name = c.getString(c.getColumnIndex("name")); 
     final String addressValue = c.getString(c.getColumnIndex("address")); 

     title.setText(name); 
     address.setText(addressValue); 
    } 

    // here is where you create a new view 
    @Override 
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2) 
    { 
     return inflater.inflate(R.layout.list_item, null); 
    } 

} 
2

아무도 API 레벨 11 이상으로 수행하는 방법을 설명하지 못합니다.

문서는 int flags 추가 매개 변수를 사용하려고하는 하나와 동일한 showing you a non-deprecated constructor으로한다. 사용할 수있는 플래그 값이없는 경우 플래그에 0을 전달하십시오.

관련 문제