Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击GridView的项时未发生任何事件_Java_Android_Gridview - Fatal编程技术网

Java 单击GridView的项时未发生任何事件

Java 单击GridView的项时未发生任何事件,java,android,gridview,Java,Android,Gridview,我使用GridView来显示类别,我在Grid View中使用了ImageButton和文本视图,但我无法在这些项目上设置OnItemClickListener(单击它时,我会看到什么都没有发生) 我的代码是 这是我的GridView适配器 public class MyAdapter extends BaseAdapter{ private ArrayList<String> listJewel_name; private ArrayList<Integer&

我使用GridView来显示类别,我在Grid View中使用了ImageButton和文本视图,但我无法在这些项目上设置OnItemClickListener(单击它时,我会看到什么都没有发生) 我的代码是 这是我的GridView适配器

public class MyAdapter extends BaseAdapter{
    private ArrayList<String> listJewel_name;
    private ArrayList<Integer> listJewellery;
    private Activity activity;
    //private LayoutInflater inflater;
    public MyAdapter(Activity activity,ArrayList<String> listJewel_name, ArrayList<Integer> listJewellery) {
        super();
        this.listJewel_name = listJewel_name;
        this.listJewellery = listJewellery;
        this.activity = activity;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listJewel_name.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listJewel_name.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        public ImageButton imgViewJewel;
        public TextView txtViewTitle;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if(convertView==null)
        {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.gridview, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.tvtext);
            view.imgViewJewel = (ImageButton) convertView.findViewById(R.id.picture);

            convertView.setTag(view);
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText(listJewel_name.get(position));
        view.imgViewJewel.setImageResource(listJewellery.get(position));

        return convertView;
    }
} 
公共类MyAdapter扩展了BaseAdapter{
private ArrayList listJewel_名称;
私人ArrayList珠宝;
私人活动;
//私人充气机;
公共MyAdapter(活动活动、ArrayList ListJewell\u名称、ArrayList ListJewell){
超级();
this.listJewel\u name=listJewel\u name;
this.listJewellery=listJewellery;
这个。活动=活动;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回listJewel_name.size();
}
@凌驾
公共字符串getItem(int位置){
//TODO自动生成的方法存根
返回listJewel_name.get(位置);
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回0;
}
公共静态类视图持有者
{
公共图像按钮imgViewJewel;
公共文本视图txtViewTitle;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视图持有者视图;
LayoutFlater充气器=活动。GetLayoutFlater();
if(convertView==null)
{
视图=新的ViewHolder();
convertView=充气机。充气(R.layout.gridview,null);
view.txtViewTitle=(TextView)convertView.findViewById(R.id.tvtext);
view.imgViewJewel=(ImageButton)convertView.findViewById(R.id.picture);
setTag(视图);
}
其他的
{
view=(ViewHolder)convertView.getTag();
}
view.txtViewTitle.setText(listJewel_name.get(position));
view.imgviewjewell.setImageResource(listJewellery.get(position));
返回视图;
}
} 
我的活动课是

GridView gridView;
    Button add;
    private MyAdapter mAdapter;
    private ArrayList<String> listJewel_name;
    private ArrayList<Integer> listJewellery;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_jewellery_option);
        prepareList();
         // prepared arraylist and passed it to the Adapter class
        mAdapter = new MyAdapter(this,listJewel_name, listJewellery);

        // Set custom adapter to gridview
        gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(mAdapter);

        // Implement On Item click listener
       gridView.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {
                String shri=mAdapter.getItem(position);
                Toast.makeText(SelectJewelleryOption.this, shri , Toast.LENGTH_SHORT).show();
            }
        });
    }
GridView-GridView;
按钮添加;
私用MyAdapter mAdapter;
private ArrayList listJewel_名称;
私人ArrayList珠宝;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u select\u jewellery\u选项);
准备主义者();
//准备arraylist并将其传递给适配器类
mAdapter=新的MyAdapter(此,ListJewell_名称,ListJewell);
//将自定义适配器设置为gridview
gridView=(gridView)findViewById(R.id.gridView);
setAdapter(mAdapter);
//在项目上实现单击侦听器
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共控件单击(AdapterView父对象、视图v、内部位置、,
长id){
字符串shri=mAdapter.getItem(位置);
Toast.makeText(选择JewelleryOption.this、shri、Toast.LENGTH_SHORT.show();
}
});
}
我的XML文件是

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<com.myjewelbox.SquareImageButtonView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="130dp"
    android:scaleType="fitCenter"
    android:clickable="true" />

    <TextView
        android:id="@+id/tvtext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="1dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_gravity="bottom"
        android:textColor="@color/Golden"
        android:background="#55000000"
        android:focusable="false"
        />
</FrameLayout>


我已经将这个XMl文件设置为主XMl文件的GridView,而不是直接为GridView设置ItemClickListener


view.imgViewJewel.setItemClickListener(活动)

确保
GridView布局的

GridView
无法处理可单击的项目<例如,代码>按钮
不起作用。此外,如果您有任何其他不可单击的
项目,则必须确保未设置属性:clikable=“true”

请参阅下面的链接-它与您期望的内容相同


在适配器类中,在返回行之前,在convertView上设置ClickListener

convertView.setOnClickListener(new onClickListener(){
    //In this method you can get the items same as you are getting in your
    //adapter, as follow:-

    String name=listJewel_name.get(position);

    //or you can do whatever you want as now you have the whole view that is 
    //clicked, so you can event get its children or start a new activity or 
    //whatever you want
}
return converView;

希望这有帮助。

请将您的xml更改为-

添加ASE 2行-

android:clickable=“true”

android:focusable=“false”


有关更多详细信息,请参见此处-

你能分享你的gridview布局文件吗?@Homosapiens我已经添加了CML文件,请看我也添加了我的XML外观。从子视图中移除
android:clickable
android:focusable
,只在gridview上设置。我设置了那些clicable和focusable属性,但什么都没有发生。你能分享吗xml文件?您可以将android:clickable=“false”设置为您的SquareImageButtonView并检查吗?现在它甚至没有检测到单击以前它检测到单击但什么都没有发生“以前它检测到单击但什么都没有发生”意味着什么?请检查更新并建议我@azerto00Set-ItemClickListener()在适配器的getview()中的视图上,比如----view.txtViewTitle.setText(listJewel_name.get(position));view.imgviewjewell.setImageResource(listJewellery.get(position));view.imgViewJewel.setOnClickListener(listner)返回convertView;setOnClickListener代替了setItemClickListener()**那么您做错了一些事情。只需从这里下载代码,并替换您的图像和文本-它应该可以工作。做一件事创建您的演示项目,并发送电子邮件给我manishupacc@gmail.com但是演示应该处于工作状态。事实上,这是我项目的一部分,我会把整个项目都寄给你,好吗?没有,没有,只有相关文件放在那里,没有整个项目。否则我需要更多的时间来理解我。我会在晚上检查并回复你。如果我不发送整个文件,它将无法工作,这就是为什么。。。请查收我寄给你的邮件
 <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <com.myjewelbox.SquareImageButtonView
            android:id="@+id/picture"
            android:layout_width="match_parent"
            android:layout_height="130dp"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:scaleType="fitCenter" />

        <TextView
            android:id="@+id/tvtext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#55000000"
            android:focusable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="1dp"
            android:paddingTop="10dp"
            android:textColor="@color/Golden" />

    </FrameLayout>