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.
I have a custom GridView with an ImageView and TextView in it and code is-
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 -
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. 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.
Can someone tell me how to read the particular value from 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();
}
});