我有一个gridview内容一些图像视图,我希望从这个图像视图的网格视图可以得到onclick来改变一个活动,我怎么能这样做,我尝试这个代码,但它不工作。在主要活动a初始化网格视图并向其添加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);
}
});
答案 0 :(得分: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);
}
});
答案 1 :(得分:0)
您需要将click作为GridView的项目处理,即
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do whatever you want here...
}
});