0

我有一个包含一些图像视图的网格视图,我想从这个网格视图的图像视图中获得 onclick 来更改活动,我该怎么做,我尝试了这段代码,但它不起作用。在主活动中初始化一个网格查看并向其添加imageview,然后我尝试创建onclick,但它不起作用

gridview.findViewById(R.id.image).setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
          Intent myIntent = new Intent(view.getContext(), DetailsActivity.class);
          startActivity(myIntent);
      }

  });
4

2 回答 2

1

尝试这样的事情:

gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent myIntent = new Intent(view.getContext(), DetailsActivity.class);
        startActivity(myIntent);
    }
});
于 2013-03-06T02:56:40.443 回答
0

您需要将点击作为 GridView 的一项来处理,即

    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //do whatever you want here...  
        }
    });
于 2013-03-06T02:39:32.917 回答