public class MainActivity extends Activity implements LoaderCallbacks<Cursor>{ private ListView listView=null; private Uri uri=null; private ContentResolver resolver=null; private CursorLoader cursorLoader=null; private String[] projection = new String[]{MediaStore.Images.Media._ID,MediaStore.Images.Media.BUCKET_DISPLAY_NAME}; private SimpleCursorAdapter cursorAdapter=null; private long[] ids = null; private LoaderManager loaderManager=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); //获取媒体图片的uri uri=MediaStore.Images.Media.EXTERNAL_CONTENT_URI; resolver= getContentResolver(); cursorLoader = new CursorLoader(this, uri, projection, null, null, null); cursorAdapter= new SimpleCursorAdapter(this, R.layout.item,null, projection, new int[]{R.id.textView_id,R.id.textView_name}, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listView.setAdapter(cursorAdapter); //获取得到所有的条目的id ids=listView.getCheckedItemIds(); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ids = listView.getCheckedItemIds(); TextView textView_id = (TextView) view.findViewById(R.id.textView_id); TextView textView_name = (TextView) view.findViewById(R.id.textView_name); boolean flg = false; for (int i = 0; i < ids.length; i++) { if (id==ids[i]) { textView_id.setTextColor((Color.BLUE)); textView_name.setTextColor(Color.BLUE); flg=true; } } if (!flg) { textView_id.setTextColor((Color.BLACK)); textView_name.setTextColor(Color.BLACK); } } }); loaderManager = getLoaderManager(); loaderManager.initLoader(2, null, this); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { // TODO Auto-generated method stub return cursorLoader; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // TODO Auto-generated method stub cursorAdapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> loader) { // TODO Auto-generated method stub cursorAdapter.swapCursor(null); } }
原文:http://www.cnblogs.com/qcgAd/p/5214310.html