簡単な例アクティビティ(着信音など)。
MyActivity.java
public class MyActivity extends Activity {
private MyCursorAdapter mAdapter;
// that's what we want to know from the database
private static final String[] PROJECTION = new String[] {
MediaStore.Audio.AudioColumns._ID, // 0 - _id must be present
MediaStore.Audio.AudioColumns.TITLE, // 1
MediaStore.Audio.AudioColumns.DATA // 2
};
// those from above - no need for cursor.getColumnIndex()
private static final int TITLE_IDX = 1;
private static final int TEXT_IDX = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView) findViewById(R.id.list_view);
mAdapter = new MyCursorAdapter(this, TITLE_IDX, TEXT_IDX);
lv.setAdapter(mAdapter);
loadContent();
}
// would be better to do in a Loader, AsyncTask, ...
private void loadContent() {
ContentResolver cr = getContentResolver();
Cursor c = cr.query(
MediaStore.Audio.Media.INTERNAL_CONTENT_URI,
PROJECTION, null, null, null
);
mAdapter.changeCursor(c);
}
}
MyCursorAdapter.java
このクラスではカーソルには本当の依存性はありません、それははるかにSimpleCursorAdapter
public class MyCursorAdapter extends CursorAdapter {
private final LayoutInflater mInflater;
private final int mTitleIdx, mTextIdx;
/**
* Creates a new MyCursorAdapter. Set cursor via changeCursor/swapCursor
* @param context <code>this</code> will usually do
* @param titleColumnIdx cursor columnindex to be displayed as title
* @param textColumnIdx cursor columnindex to be displayed as text below
*/
public MyCursorAdapter(Context context, int titleColumnIdx, int textColumnIdx) {
super(context, null, false);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mTitleIdx = titleColumnIdx;
mTextIdx = textColumnIdx;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.title);
TextView text = (TextView) view.findViewById(R.id.text);
title.setText(cursor.getString(mTitleIdx));
text.setText(cursor.getString(mTextIdx));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View item = mInflater.inflate(R.layout.list_item, null);
// could do static init here/attach holder/set onClickListeners, ...
return item;
}
}
メインのようなものです。 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- Preview: [email protected]/list_item -->
</ListView>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Large"
android:textColor="@android:color/primary_text_dark"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Small"
android:textColor="@android:color/secondary_text_dark"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginBottom="5dp" />
</LinearLayout>
あなたは
あなたはあなたがビューを操作することができます本物の 'CursorAdapter'を使用することができます何を得る自分ます。http: //thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/ – zapl
Mmm ...ありがとう!私はそれを試してみよう! – fvss
その例についての1つのこと: 'SimpleCursorAdaper'を拡張しないで、代わりに' CursorAdapter'を使用してください。 – zapl