如何转接器和活动之间进行通信 [英] How communicate between Adapter and Activity

查看:126
本文介绍了如何转接器和活动之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道什么是从适配器连接到相应的活动进行沟通的最佳方式。

I'm not sure what's the best way to communicate from an Adapter to the corresponding Activity.

我的活动有一个web视图的布局,并在其上​​一集:

My activity has a layout with a WebView and a Gallery on top of it:

<?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>

活动加载图像数据到画廊:

The activity loads image data into the Gallery:

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

在这些图片的点击应该改变web视图的内容。下面是与OnClickListener适配器:

A click on one of these images should change the contents of the webview. Here's the adapter with the 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;
  }
}

现在我的问题是。我如何通知哪些图像已被点击和活动转移的一个对象的活动呢?

Now my question is. How do I notify the activity about what image has been clicked on and transfer one object to the activity?

我的想法是使用一个BroadcastReceiver,但我不认为这是prefered的方式来做到这一点。我该怎么办?

My thoughts are to use a BroadcastReceiver but I don't think that this is the prefered way to do so. What should I do?

在预先感谢。

推荐答案

而不是创建的ImageView点击监听器,使用画廊的setOnItemClickListener

Instead creating Imageview's click listener, use gallery's setOnItemClickListener

 yourGallery.setOnItemClickListener(new OnItemClickListener() 
 {
     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {

         system.out.println("position"+position);               

     }
 });

这篇关于如何转接器和活动之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆