问题描述
我已经找到很多答案对SO和其他网站如何填充微调
与光标
,但它们都使用pctated去$ p $ SimpleCursorAdapter(背景下,INT的String [],INT [])
构造函数来做到这一点。似乎没有人描述如何使用API级别11以上的做到这一点。
I have found lots of answers on SO and other websites about how to fill a Spinner
with a Cursor
, but all of them use the deprectated SimpleCursorAdapter(Context, int, String[], int[])
constructor to do that. No one seems to describe how to do it with API level 11 and above.
该API告诉我使用 LoaderManager
,但我不知道如何使用。
The API tells me to use the LoaderManager
, but I'm not sure about how to use that.
推荐答案
我建议实现自己的CursorAdapter,而不是使用SimpleCursorAdapter。
I would suggest implementing your own CursorAdapter instead of using SimpleCursorAdapter.
实现一个CursorAdapter的并不比实施任何其他适配器更难。
CursorAdapter的延伸BaseAdapter和getItem()时,getItemId()方法已经重写你和返回的实际值。
我们推荐使用从支持库(android.support.v4.widget.CursorAdapter)CursorAdapter的,如果你不支持pre-蜂窝。如果你只有11后的,只需要使用android.widget.CursorAdapter
记住,你不需要调用notifyDataSetChanged(),当你调用swapCursor(newCursor);
Implementing a CursorAdapter is no harder than implementing any other Adapter.CursorAdapter extends BaseAdapter, and getItem(), getItemId() methods are already overriden for you and return the real values.It's recommended to use the CursorAdapter from support library (android.support.v4.widget.CursorAdapter) if you do support pre-Honeycomb. If you are only after 11, just use the android.widget.CursorAdapterMind that you don't need to call notifyDataSetChanged() when you call swapCursor(newCursor);
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);
}
}
这篇关于如何填补API级别11后光标的微调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!