Android 加载系统图库

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);
        
    }
    
    
}

 

转载于:https://www.cnblogs.com/qcgAd/p/5214310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值