需要把ExpandableListView的适配器(继承自BaseExpandableListAdapter)同时绑定到AutoCompleteTextView,实现搜索过滤功能。实现了接口Filterable,以为成功在即,既然编译抛错误
The generic method setAdapter(T) of type AutoCompleteTextView is not applicable for the arguments (MyExpandableListAdapter). The inferred type MyExpandableListAdapter is not a valid substitute for the bounded parameter <T extends ListAdapter & Filterable>
MyExpandableListAdapter未实现ListAdapter,跟进BaseExpandableListAdapter最终继承的接口ExpandableListAdapter既然不是ListAdapter的子类!ExpandableListView继承自ListView google既然让它实现不同的适配器,哦!狗屎!
继续看看setAdapter有两个重载
public void setAdapter(ExpandableListAdapter adapter) public void setAdapter(ListAdapter adapter)
如果调用
public void setAdapter(ListAdapter adapter)
直接就抛异常。看另外一个函数的实现
public void setAdapter(ExpandableListAdapter adapter) { // Set member variable mAdapter = adapter; if (adapter != null) { // Create the connector mConnector = new ExpandableListConnector(adapter); } else { mConnector = null; } // Link the ListView (superclass) to the expandable list data through the connector super.setAdapter(mConnector); }
看,google既然在ExpandableListAdapter和ListAdapter直接做一个链接器来适配ListView。
ExpandableListView的这种实现方法,虽然可重用listview,但函数setAdapter签名和父类ListView不统一,无疑给调用者添加陷阱和增加复杂度。
回到上面的问题:如果需要MyExpandableListAdapter同时能够被AutoCompleteTextView做adapter,只好乖乖实现ListAdapter,所以要实现ListAdapter、ExpandableListAdapter两个接口。这么来编译还是不能通过。
For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of " + "setAdapter(ListAdapter)
聪明的java编译器把MyExpandableListAdapter认为是ListAdapter的子类了。怎么办?很简单!一个小技巧让编译器更加聪明点
expandableListView.setAdapter((ExpandableListAdapter)MyExpandableListAdapter);
至此MyExpandableListAdapter就不仅可以被ExpandableListView使用而且可以被AutoCompleteTextView使用了。
希望此文对您有用!
http://blog.csdn.net/xiaonamylove/article/details/3939965
CharSequence[] items = {"相册", "相机"}; new AlertDialog.Builder(this) .setTitle("选择图片来源") .setItems(items, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { if( which == SELECT_PICTURE ){ Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "选择图片"), SELECT_PICTURE); }else{ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, SELECT_CAMER); } } }) .create().show();
处理图片,方法一,直接处理返回图片:
注释:
1、网上有说明,直接处理返回的图片是被系统压缩过的,不过自己在测试的过程并没有区别;
2、如果用户不断的重新获取图片的话,必须把现在的Bmp内存释放,否则会报错! bmp.recycle()。
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK){ //选择图片 Uri uri = data.getData(); ContentResolver cr = this.getContentResolver(); try { if(bmp != null)//如果不释放的话,不断取图片,将会内存不够 bmp.recycle(); bmp = BitmapFactory.decodeStream(cr.openInputStream(uri)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("the bmp toString: " + bmp); imageSV.setBmp(bmp); }else{ Toast.makeText(SetImageActivity.this, "请重新选择图片", Toast.LENGTH_SHORT).show(); }
处理图片,方法二,获得图片的地址再处理:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK){ Uri uri = data.getData(); String [] proj={MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery( uri, proj, // Which columns to return null, // WHERE clause; which rows to return (all rows) null, // WHERE clause selection arguments (none) null); // Order-by clause (ascending by name) int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); bmp = BitmapFactory.decodeFile(path); System.out.println("the path is :" + path); }else{ Toast.makeText(SetImageActivity.this, "请重新选择图片", Toast.LENGTH_SHORT).show(); } } }