简体   繁体   English

如何从android中的自定义GridView中的选定行中获取项目?

[英]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.

相关问题 如何使用ImageAdapter从Gridview中获取所选项目? (机器人) - How do I get the selected item from a Gridview with ImageAdapter? (Android) 如何从GridView中的选定项目中获取数据? - How to get data from selected item in GridView? Android如何在不使用onclicklistner的情况下从gridview获取所选项目的位置,而是使用ontouchlistner - Android How to get position of selected item from gridview without using onclicklistner, using ontouchlistner instead ANDROID-如何从字符串中保存从gridView中选择的项目? - ANDROID- how to save selected item from gridView in a string? 如何从自定义ListView获取所选项目? - How to get selected item from custom ListView? 如何从Adapter类中设置自定义gridview获取项目位置? - How to get the item position from custom gridview set in an Adapter class? 无法从android中的自定义列表视图中获取所选项目 - Unable to get selected item from custom listview in android 如何在使用Custom BaseAdapter在Android中选择/取消选择gridView项目时显示/隐藏布局 - How to show/hide a layout when gridView item is selected/unselected in Android using Custom BaseAdapter 如何从 Android 中的 gridview 获取项目的背景颜色? - How can I get the background color of an item from gridview in Android? Android gridview保持选中项目 - Android gridview keep item selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM