当我想使用setonItemClickListener时,请使用以下内容
GrIDVIEw grIDvIEw = (GrIDVIEw) findVIEwByID(R.ID.grIDvIEw);grIDvIEw.setAdapter(new ImageAdapter(Tab1Fragment.getContext(null)));grIDvIEw.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent, VIEw v, int position, long ID) { // Send intent to SingleVIEwActivity Intent i = new Intent(getApplicationContext(), SingleVIEwActivity.class); // Pass image index i.putExtra("ID", position); startActivity(i); } });
但是对于下面的代码,我不能像上面那样:
public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.grIDvIEw1, null); GrIDVIEw ListVIEw = (GrIDVIEw) vIEw.findVIEwByID(R.ID.mainGrID1); ListVIEw.setAdapter(new Adapter()); GrIDVIEw.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent, VIEw v, int position, long ID) { // Send intent to SingleVIEwActivity Intent i = new Intent(getApplicationContext(), SingleVIEwActivity.class); // Pass image index i.putExtra("ID", position); startActivity(i); } }); return vIEw;}
这是错误图片
解决方法:
为什么使用类名GrIDvIEw而不是已声明为ListvIEw的grIDvIEw变量
错误代码:-
GrIDVIEw.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent, VIEw v, int position, long ID) { // Send intent to SingleVIEwActivity Intent i = new Intent(getApplicationContext(), SingleVIEwActivity.class); // Pass image index i.putExtra("ID", position); startActivity(i); } });
代替上面的代码将其更改为
ListVIEw.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent, VIEw v, int position, long ID) { // Send intent to SingleVIEwActivity Intent i = new Intent(getApplicationContext(), SingleVIEwActivity.class); // Pass image index i.putExtra("ID", position); startActivity(i); } });
总结 以上是内存溢出为你收集整理的android-片段和图像视图中的setOnItemClickListener全部内容,希望文章能够帮你解决android-片段和图像视图中的setOnItemClickListener所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)