[英]How to get item from selected row in custom GridView in android?
I was looking at this question for a solution to my question( How do I get the selected item from a Gridview with ImageAdapter? (Android) ), but I can't find any. 我一直在寻找这个问题的解决方案(我如何使用ImageAdapter从Gridview中获取所选项目?(Android) ),但找不到任何问题。
I have a custom GridView with an ImageView and TextView in it and code is- 我有一个带有ImageView和TextView的自定义GridView,代码是-
public class ListArray extends BaseAdapter {
Context con;
List<String> obj;
private final int[] Imageid;
public ListArray(Context con, List<String> obj, int[] imageId) {
this.con = con;
this.obj = obj;
this.Imageid = imageId;
}
@Override
public int getCount() {
return obj.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inf = (LayoutInflater) con
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.liststyle, null);
TextView tv = (TextView) convertView.findViewById(R.id.textViewls1);
ImageView imageView = (ImageView) convertView
.findViewById(R.id.imageViewls1);
tv.setText(obj.get(position));
imageView.setImageResource(Imageid[0]);
return convertView;
}
}
This is my Gridview - 这是我的Gridview-
Now I want to read the value in the TextView when clicked on a particular row, for example, I want to get Group1 when clicked on 1st row. 现在,我想在单击特定行时读取TextView中的值,例如,当我单击第一行时,要获取Group1。 So I coded the following -
所以我编写了以下代码-
gv.setOnItemClickListener(new OnItemClickListener() {
@SuppressLint("NewApi")
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(),
"" + arg0.getSelectedItem(), Toast.LENGTH_SHORT)
.show();
}
});
Besides getSelectedItem() I tried some other functions along with arg0, but nothing gave me the desired result. 除了getSelectedItem()之外,我还尝试了其他一些与arg0一起使用的函数,但没有任何结果给我带来期望的结果。
Can someone tell me how to read the particular value from Gridview ? 有人可以告诉我如何从Gridview中读取特定值吗?
Something like this should work: 这样的事情应该起作用:
gv.setOnItemClickListener(new OnItemClickListener() {
@SuppressLint("NewApi")
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv =(TextView) arg1.findViewById(R.id.textViewInYourList);//your textview id
Toast.makeText(myContext,
"" + tv.getText().toString(), Toast.LENGTH_SHORT)
.show();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.