问题描述
我怎样才能得到一个网格视图
的项目标签是由它的位置,并有从另一个来获得网格视图
的项目点击监听功能。
gridview2.setOnItemClickListener(新OnItemClickListener()
{
公共无效onItemClick(适配器视图<>母公司,视图V,
INT位置,长的ID)
{
INT I =(整数)gridview1.getChildAt(gridview1s_position).getTag(); - >它返回null
}
});
MY gridview1.setOnitemClicklistener和gridview2.setonitemclicklistener是在相同的文件(MainActivity.java)
但gridview1适配器是ImageAdapter.java(这是在gridview1设置图像)和
gridview2适配器imageadapter2.java(这是用于设置黑色图像,使得
gridview2看起来像行的框和
列,否则它显示像粗线,因为我们没有把GridView中的任何东西)
gridview1.setOnItemClickListener(新OnItemClickListener()
{
公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的ID)
{
INT标记=(整数)gridview1.getChildAt(位置).getTag(); //返回这个标签
}
});
ImageAdapter1.java
公共查看getView(INT为arg0,ARG1查看,ViewGroup中ARG2){
// TODO自动生成方法存根
如果(ARG1!= NULL)
{
IMV =(ImageView的)ARG1;
}
其他
{
IMV =新ImageView的(续)
imv.setLayoutParams(新GridView.LayoutParams(40,40));
imv.setScaleType(ScaleType.CENTER_CROP);
(0,0,0,0)imv.setPadding; }
imv.setImageResource(imageid [arg0中]); // imageid阿雷是具有可拉伸的图像
imv.setTag(imageid [arg0中]);
返回IMV;}
gridview2
gridview2.setOnItemClickListener(新OnItemClickListener()
{
ImageAdapter IA =新ImageAdapter(getApplicationContext());
公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的ID)
{
ImageView的IMV;
如果(V!= NULL)
{
IMV =(ImageView的)V;
}
其他
{
IMV =新ImageView的(getApplicationContext());
imv.setLayoutParams(新GridView.LayoutParams(40,40));
imv.setScaleType(ScaleType.CENTER_CROP);
(0,0,0,0)imv.setPadding;
imv.setBackgroundColor(5555); } imv.setImageResource(ia.imageid [TT]);
}
});
最后我自己我有我的question.Thank您的解决方案为所有谁给了他们宝贵的意见。
我保存的网厂景的一个变量particullar细胞的标记和使用的变量在网格视图2。
How can I get a grid-view
's item tag by its position and have to get from another grid-view
's item click listener function.
gridview2.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
int i=(Integer)gridview1.getChildAt(gridview1s_position).getTag();---> it is returning Null
}
});
MY gridview1.setOnitemClicklistener and gridview2.setonitemclicklistener are in the same file (MainActivity.java)
But the gridview1 adapter is a ImageAdapter.java(this is for setting the images in the gridview1) and
gridview2 adapter is imageadapter2.java(this is for setting black images so that the
gridview2 looks like a box with rows and
columns, otherwise it is showing like a Bold line because we didn't put anything in the gridview)
gridview1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
int tag=(Integer)gridview1.getChildAt(position).getTag();//this returning the tag
}
});
ImageAdapter1.java
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
if(arg1!=null)
{
imv=(ImageView)arg1;
}
else
{
imv=new ImageView(cont);
imv.setLayoutParams(new GridView.LayoutParams(40,40));
imv.setScaleType(ScaleType.CENTER_CROP);
imv.setPadding(0,0,0,0);
}
imv.setImageResource(imageid[arg0]);//imageid aray is having drawable images
imv.setTag(imageid[arg0]);
return imv;
}
gridview2
gridview2.setOnItemClickListener(new OnItemClickListener()
{
ImageAdapter ia=new ImageAdapter(getApplicationContext());
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
ImageView imv;
if(v!=null)
{
imv=(ImageView)v;
}
else
{
imv=new ImageView(getApplicationContext());
imv.setLayoutParams(new GridView.LayoutParams(40,40));
imv.setScaleType(ScaleType.CENTER_CROP);
imv.setPadding(0,0,0,0);
imv.setBackgroundColor(5555);
}
imv.setImageResource(ia.imageid[tt]);
}
});
Finally myself i got the solution for my question.Thank you for all who gave their valuable suggestions.
i saved the tag of a particullar cell of grid-view1 in a variable and used that variable in the grid-view2.
这篇关于Android的:如何通过它的位置从获得网格视图项目标签:在另一个网格视图设置的项目,点击监听功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!