【发布时间】:2014-09-16 06:08:18
【问题描述】:
我正在寻找这个问题的解决方案(How do I get the selected item from a Gridview with ImageAdapter? (Android)),但我找不到任何问题。
我有一个自定义的 GridView,里面有一个 ImageView 和 TextView,代码是-
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;
}
}
这是我的 Gridview -
现在我想在单击特定行时读取 TextView 中的值,例如,我想在单击第一行时获取 Group1。所以我编写了以下代码 -
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();
}
});
除了 getSelectedItem() 之外,我还尝试了一些其他函数以及 arg0,但没有给我想要的结果。
谁能告诉我如何从 Gridview 读取特定值?
【问题讨论】:
-
您也可以在单击列表项时从 arraylist 中获取项目。
-
@PG_Android 怎么样?请将其描述为答案。
-
你想得到什么?表示“Group1”之类的项目名称?
-
@PG_Android 我得到了 null,行数(0 和 1),类似 android:layout@..... 但是当我点击特定的行时我没有得到 Group1 或 Poly1。
标签: android gridview onclick onitemclick