Android 如何在适配器和活动之间进行通信
我不确定从适配器到相应活动的最佳通信方式是什么 “我的活动”有一个布局,上面有一个网络视图和一个图库:Android 如何在适配器和活动之间进行通信,android,android-activity,adapter,Android,Android Activity,Adapter,我不确定从适配器到相应活动的最佳通信方式是什么 “我的活动”有一个布局,上面有一个网络视图和一个图库: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_p
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent"
android:spacing="2dip" />
<WebView
android:id="@+id/webview"
android:layout_height="wrap_content"
android:layout_weight="5"
android:layout_width="fill_parent" />
</LinearLayout>
该活动将图像数据加载到库中:
public class MyActivity extends Activity {
private MyAdapter adapter;
private Container container;
private ArrayList<Container> containers = new ArrayList<Container>();
private Gallery gallery;
private WebView webView;
@Override
public void onCreate(Bundle bundle) {
...
gallery = (Gallery) findViewById(R.id.gallery);
webView = (WebView) findViewById(R.id.webview);
containers = fetchContainers();
doGallery();
}
private void doGallery() {
adapter = new MyAdapter(this,
containers);
gallery.setAdapter(adapter);
}
private ArrayList<Container> fetchContainers() {
ArrayList<Container> containers = null;
...
return containers;
}
}
公共类MyActivity扩展活动{
专用MyAdapter适配器;
私人货柜;
私有ArrayList容器=新的ArrayList();
私人画廊;
私有网络视图;
@凌驾
创建公共void(Bundle){
...
画廊=(画廊)findViewById(R.id.gallery);
webView=(webView)findviewbyd(R.id.webView);
containers=fetchContainers();
doGallery();
}
私人空房{
适配器=新的MyAdapter(此,
容器);
gallery.setAdapter(适配器);
}
私有ArrayList fetchContainers(){
ArrayList容器=null;
...
返回容器;
}
}
单击其中一个图像将更改webview的内容。以下是带有OnClickListener的适配器:
public class MyAdapter extends BaseAdapter {
private class MyOnClickListener implements OnClickListener {
private Container container;
private Context context;
public MyOnClickListener(Context context, Container container) {
this.container = container;
this.context = context;
}
public void onClick(View view) {
//TODO: change contents of WebView
}
}
private ArrayList<Container> containers;
private Context context;
public View getView(final int position, final View contentView, final ViewGroup viewGroup) {
ImageView imageView = new ImageView(context);
Container container = containers.get(position);
// get image data from image cache
imageView.setOnClickListener(new MyOnClickListener(context, container));
return imageView;
}
public MyAdapter(Context context, ArrayList<Container> containers) {
this.containers = containers;
this.context = context;
}
}
公共类MyAdapter扩展了BaseAdapter{
私有类MyOnClickListener实现OnClickListener{
私人货柜;
私人语境;
公共MyOnClickListener(上下文、容器){
this.container=容器;
this.context=上下文;
}
公共void onClick(视图){
//TODO:更改WebView的内容
}
}
私有ArrayList容器;
私人语境;
公共视图getView(最终整型位置、最终视图内容视图、最终视图组视图组){
ImageView ImageView=新的ImageView(上下文);
Container=containers.get(位置);
//从图像缓存中获取图像数据
setOnClickListener(新的MyOnClickListener(上下文,容器));
返回图像视图;
}
公共MyAdapter(上下文、ArrayList容器){
这个。容器=容器;
this.context=上下文;
}
}
现在我的问题是。如何通知活动已单击的图像,并将一个对象传送到活动
我的想法是使用广播接收器,但我不认为这是最好的方式。我该怎么办
非常感谢。在活动中使用Gallery对象的
setOnItemClickListener
方法。使用Gallery的setOnItemClickListener,而不是创建Imageview的click listener
yourGallery.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {
system.out.println("position"+position);
}
});
yourGallery.setOnItemClickListener(新的OnItemClickListener()
{
@凌驾
public void onItemClick(适配器视图arg0、视图arg1、内部位置、长arg3){
系统输出打印项次(“位置”+位置);
}
});