【问题标题】:How communicate between Adapter and ActivityAdapter和Activity之间如何通信
【发布时间】:2011-08-06 08:21:12
【问题描述】:

我不确定从 Adapter 到相应 Activity 的最佳通信方式是什么。

我的活动有一个布局,上面有一个 WebView 和一个画廊:

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

单击这些图像之一应该会更改 web 视图的内容。这是带有 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;
  }
}

现在我的问题是。如何通知 Activity 哪些图像被点击并将一个对象转移到 Activity?

我的想法是使用 BroadcastReceiver,但我不认为这是首选的方法。我该怎么办?

非常感谢。

【问题讨论】:

    标签: android android-activity adapter


    【解决方案1】:

    使用画廊的 setOnItemClickListener 代替创建 Imageview 的点击监听器

     yourGallery.setOnItemClickListener(new OnItemClickListener() 
     {
         @Override
         public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {
    
             system.out.println("position"+position);               
    
         }
     });
    

    【讨论】:

      【解决方案2】:

      在 Activity 中使用 Gallery 对象的 setOnItemClickListener 方法。

      【讨论】:

        猜你喜欢
        • 2014-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-20
        • 1970-01-01
        • 1970-01-01
        • 2013-01-19
        相关资源
        最近更新 更多