姓、名の順に並べ替えられた連絡先の listView を表示するアプリケーションがあります。各連絡先の横には画像 (アイコン) があります。3 つの異なる画像 (顧客/サプライヤー/その他) を表示したい 3 種類の連絡先があります。現在、顧客に設定されている既定の画像があります。以下に示す cusorLoader を使用してオンザフライで画像を切り替える方法があるかどうか、または onResume にカーソルを含むメソッドを追加するのが最善かどうか疑問に思っています。(画像を表示する必要があるたびに onResume が呼び出されます)。simpleCursorAdapter は textViews のみを引数として取ることができると信じているので、可能であれば、複合テキストビュー/画像が機能する可能性があります。私のアイコンはデータベースには保存されず、ドローアブルにのみ保存されます。
返信ありがとうございます。
@Override
protected void onResume() {
super.onResume();
//Starts a new or restarts an existing Loader in this manager
getLoaderManager().restartLoader(0, null, this);
}
/*
* The fillData method binds the simpleCursorAadapter to the listView.
*/
private void fillData() {
String[] from = new String[] { ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };
//The XML views that the data will be bound to:
int[] to = new int[] {R.id.label2, R.id.label};
getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.contact_row, null, from,
to, 0);
setListAdapter(adapter);
}
// Sort the names by last name, then by first name
String orderBy = ContactsDB.COLUMN_LAST_NAME + " COLLATE NOCASE ASC"
+ "," + ContactsDB.COLUMN_FIRST_NAME + " COLLATE NOCASE ASC" ;
// Creates a new loader after the initLoader () call
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { ContactsDB.ROW_ID, ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };
CursorLoader cursorLoader = new CursorLoader(this,
SomeContentProvider.CONTENT_URI, projection, null, null, orderBy);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in.
// (The framework will take care of closing the old cursor once we return.)
adapter.swapCursor(data); //Call requires Min API 11
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed.
// Data is no longer available, delete the reference
adapter.swapCursor(null);
}
}