- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 GridView ,在 fragment 中显示了图像和关联的复选框。 Fragment 类包括从 Gallery 或相机捕获图像的方法。如何使用已单击的项目引用引入从 Adapter 到 Fragment 类的回调,以便可以从 Fragment 类执行捕获/加载图像?
必须加载从相机或图库中捕获的图像的网格的 fragment 类:
public class SellMainFrag extends Fragment{
private int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private Button btnSelect;
private ImageView ivImage;
private String userChoosenTask;
public SellMainFrag() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sell_main, container, false);
int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
ImageGridAdapter adapter = new ImageGridAdapter(view.getContext(), iconSize);
GridView gridview = (GridView) view.findViewById(R.id.gridView1);
gridview.setAdapter(adapter);
return view;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(userChoosenTask.equals("Take Photo"))
cameraIntent();
else if(userChoosenTask.equals("Choose from Library"))
galleryIntent();
} else {
//code for deny
}
break;
}
}
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result=Utility.checkPermission(getContext());
if (items[item].equals("Take Photo")) {
userChoosenTask ="Take Photo";
if(result)
cameraIntent();
} else if (items[item].equals("Choose from Library")) {
userChoosenTask ="Choose from Library";
if(result)
galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void galleryIntent()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}
private void cameraIntent()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ivImage.setImageBitmap(thumbnail);
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
ivImage.setImageBitmap(bm);
}
}
包含用于捕获点击的监听器的适配器类:
public class ImageGridAdapter extends BaseAdapter{
private Context mContext;
private int mIconSize;
private static LayoutInflater inflater=null;
private String[] web = {
"Doctors","Movies","Dentists","Restaurants","Car"} ;
private int[] imageId = {
R.mipmap.doctors,
R.mipmap.dentists,
R.mipmap.restaurants,
R.mipmap.carmechanic,
R.mipmap.ic_launcher
};
public ImageGridAdapter(Context mContext, int mIconSize) {
super();
this.mContext = mContext;
this.mIconSize = mIconSize;
inflater = ( LayoutInflater )mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageId.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public class Holder
{
CheckBox ck;
ImageView img;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.sell_main_images, null);
holder.ck=(CheckBox) rowView.findViewById(R.id.chk1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.ck.isChecked();
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// How do I introduce a callback to Fragment class with the item reference that has been clicked so that capture/loading images can be performed from the Fragment class?
}
});
return rowView;
}
}
最佳答案
你可以这样做:
图像网格适配器
public class ImageGridAdapter extends BaseAdapter{
private Context mContext;
private int mIconSize;
private static LayoutInflater inflater=null;
AdapterCallback callback;
public interface AdapterCallback{
void onItemClicked(int position);
}
private String[] web = {
"Doctors","Movies","Dentists","Restaurants","Car"} ;
private int[] imageId = {
R.mipmap.doctors,
R.mipmap.dentists,
R.mipmap.restaurants,
R.mipmap.carmechanic,
R.mipmap.ic_launcher
};
public ImageGridAdapter(Context mContext, int mIconSize, AdapterCallback callback) {
super();
this.callback = callback;
this.mContext = mContext;
this.mIconSize = mIconSize;
inflater = ( LayoutInflater )mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageId.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public class Holder
{
CheckBox ck;
ImageView img;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.sell_main_images, null);
holder.ck=(CheckBox) rowView.findViewById(R.id.chk1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.ck.isChecked();
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// How do I introduce a callback to Fragment class with the item reference that has been clicked so that capture/loading images can be performed from the Fragment class?
if(callback != null) {
callback.onItemClicked(position);
}
}
});
return rowView;
}
}
和 fragment
public class SellMainFrag extends Fragment implements AdapterCallback{
private int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private Button btnSelect;
private ImageView ivImage;
private String userChoosenTask;
public SellMainFrag() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sell_main, container, false);
int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
ImageGridAdapter adapter = new ImageGridAdapter(view.getContext(), iconSize, SellMainFrag.this);
GridView gridview = (GridView) view.findViewById(R.id.gridView1);
gridview.setAdapter(adapter);
return view;
}
@Override
public void onItemClicked(int position){
// call back here
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(userChoosenTask.equals("Take Photo"))
cameraIntent();
else if(userChoosenTask.equals("Choose from Library"))
galleryIntent();
} else {
//code for deny
}
break;
}
}
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result=Utility.checkPermission(getContext());
if (items[item].equals("Take Photo")) {
userChoosenTask ="Take Photo";
if(result)
cameraIntent();
} else if (items[item].equals("Choose from Library")) {
userChoosenTask ="Choose from Library";
if(result)
galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void galleryIntent()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}
private void cameraIntent()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ivImage.setImageBitmap(thumbnail);
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
ivImage.setImageBitmap(bm);
}
}
关于android - 如何从 Adapter 返回回调到 Fragment 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39715025/
我正在制作一个 android 应用程序,它允许用户在 editText 中输入关键字,当他们点击提交时,下面的 recyclerview 将显示来自 API 请求的结果。 我的 recyclerVi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我想在我的 Svelte Kit 应用程序中使用 @sveltejs/adapter-static(想把它变成一个 SPA)。 我使用 npm i @sveltejs/adapter-static 安
我有一个简单的表单,在提交重定向到 AuthController 内的 processAction 之后,在这个 action 中我想要创建一个简单的 table bar。 已编辑: 引用Zend f
当我使用 ListView 时,我扩展了 ArrayAdapter 并将数据(一个列表)存储在 ArrayAdapter 中,因此当数据更改时,我调用: adapter.add(item).; 还有另
我有一个使用 Webpack 捆绑的 commonjs 浏览器应用程序( typescript )。它使用 webrtc,所以我想使用 webrtc-adapter package from npm
在 “DesignPatterns: Elements of Reusable Object-Oriented Software” 一书中谈到 C++ 实现 Adapter 模式时,它是这样的这个:
只需要帮助理解 ZF2 中的一些简单数据库查询。在 ZF1 中,我有这样简单的方法: public function recordset() { // listing of all records $
当使用Spring的Java配置时,你一定会看到像WebMvcConfigurerAdapter这样的类& HandlerInterceptorAdapter它们实现单个接口(interface),并
我将 doctrine2 与 ZF2 一起使用,我的一些库与 Zend\Db\Adapter\Adapter 一起使用,其他的与 doctrine2 一起使用。现在,他们两次连接到数据库。是否可以在原
过去几天我一直在工作灯上工作,现在正在尝试制作连接到数据库的应用程序。以下是截图: 1) Worklight\server\conf\worklight.properties 2) mySQLAdap
几年前,我在 this tutorial 之后开始学习 Zend 框架。 .在那里,它显示映射器是使用 Zend\Db\Adapter\Adapter 创建的。类来获取数据库连接,这就是我使用数据库的
我正在学习 RecyclerView 并在 developer 中的站点 Adapter 类扩展 RecyclerView.Adapter 。实现显示: public class MyAdapter
查看文档:http://docs.spring.io/spring-integration/reference/html/ip.html#tcp-adapters我知道适配器仅用于单向通信。但是,这里
当我移动到另一个 fragment 时,我如何恢复分页适配器项状态?我试过下面的文章,但没有用。 https://medium.com/@florina.muntenescu private fun
我正在使用 capistrano 并收到此错误消息: Please install the pg adapter: `gem install activerecord-pg-adapter` (can
我正在尝试在本地主机上安装 Magento 2,当我想连接数据库时出现错误。 错误是: “Magento\Framework\DB\Adapter\Pdo\MysqlFactory”生成的源类“\Ma
我的 Rails 服务器通过正常的开发脚本运行良好,我计划使用 Netbeans for Rails。当我使用 Netbeans 启动我现有的项目并重新启动我的 Webrick 服务器时,它显示错误
运行 Ruby on Rails (RoR) 应用程序或使用 ActiveRecord 框架的 Ruby 代码,您会收到错误消息: Please install the postgresql adap
我在运行 Redmine2.3-stable 时遇到这个错误,随后无法成功安装 mysql2 适配器。 这是错误: Please install the mysql2 adapter: `ge
我是一名优秀的程序员,十分优秀!