开发者

How communicate between Adapter and Activity

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

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

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 con开发者_JAVA百科tents 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?

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?

Many thanks in advance.


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

     }
 });


Use the setOnItemClickListener method of your Gallery object in the activity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜