当前位置: 首页 > 知识库问答 >
问题:

网格视图 onclick 侦听器 它不起作用

阎宝
2023-03-14

我正在使用布局的网格视图,它在列表中的网格视图项之后不工作,它在logcat错误中显示“throwIndexOutOfBoundsException”。没有onclick函数,如果我使用toast它的工作,如果我为下一个活动提供一个onlicck函数,它将不工作

Gridviewadapter.java

public class GridviewAdapter extends BaseAdapter
{
    private ArrayList<String> listginfy;
    private ArrayList<Integer> listimage;
    private Activity activity;

    public GridviewAdapter(GinfyActivity ginfyActivity,ArrayList<String> listginfy, ArrayList<Integer> listimage) {
        super();
        this.listginfy = listginfy;
        this.listimage = listimage;
        this.activity = (Activity) ginfyActivity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listginfy.size();
    }

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

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

    public static class ViewHolder
    {
        public ImageView imgViewGinfy;
        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_row, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
            view.imgViewGinfy = (ImageView) convertView.findViewById(R.id.imageView1);

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

        view.txtViewTitle.setText(listginfy.get(position));
        view.imgViewGinfy.setImageResource(listimage.get(position));

        return convertView;
    }

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


}

主活动.java

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_ginfy);

        prepareList();

        // prepared arraylist and passed it to the Adapter class
   mAdapter = new GridviewAdapter(this,listginfy, listimage);

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

        // Implement On Item click listener
        gridView.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {


                mAdapter = results.get(position);

                switch(mAdapter.getItemId())
                {
                case 1:
                Intent newActivity = new Intent(GinfyActivity.this,MainActivity.class);
                startActivity(newActivity);
                break;

                case 2:
                Intent new1Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity.class);
                startActivity(new1Activity);
                break;
                default:
                    Toast.makeText(GinfyActivity.this, "Wrong Input", Toast.LENGTH_LONG).show();
                    }
            }
        });

    }

    public void prepareList()
    {
        listginfy = new ArrayList<String>();

        listginfy.add("Prayers");
        listginfy.add("Poojaroom");


        listimage = new ArrayList<Integer>();
        listimage.add(R.drawable.ginfyprayer);
        listimage.add(R.drawable.poojaroom1);

    }


}

gridview的Onclick函数在这里不起作用,我也发布了我的xml布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/border"
    android:padding="5dp">

    <ImageView
        android:layout_height="64dp"
        android:id="@+id/imageView1"
        android:layout_width="64dp"
        android:src="@drawable/icon"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:clickable="True">
    </ImageView>

    <TextView
        android:text="TextView"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="2dp"
        android:layout_centerHorizontal="true"
        android:textSize="18sp"
        android:ellipsize="marquee"></TextView>


</RelativeLayout>

home_ginfy.xml

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

    <GridView
        android:layout_height="wrap_content"
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:numColumns="auto_fit"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:clickable="True">
    </GridView>
</LinearLayout>

共有3个答案

郎聪
2023-03-14

我一直在用这样的东西。在你的情况下,这应该是可行的。

public class GridviewAdapter extends BaseAdapter
{
    private ArrayList<String> listginfy;
    private ArrayList<Integer> listimage;
    private Activity activity;

    public GridviewAdapter(GinfyActivity ginfyActivity,ArrayList<String> listginfy, ArrayList<Integer> listimage) {
        super();
        this.listginfy = listginfy;
        this.listimage = listimage;
        this.activity = (Activity) ginfyActivity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listginfy.size();
    }

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

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

    public static class ViewHolder
    {
        public ImageView imgViewGinfy;
        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_row, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
            view.imgViewGinfy = (ImageView) convertView.findViewById(R.id.imageView1);

            convertView.setTag(view);
            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    switch (position){
                         case 1:
                              Intent newActivity = new Intent(GinfyActivity.this,MainActivity.class);
                              startActivity(newActivity);
                              break;
                         case 2:
                              Intent new1Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity.class);
                              startActivity(new1Activity);
                              break;
                         default:
                              Toast.makeText(GinfyActivity.this, "Wrong Input", Toast.LENGTH_LONG).show();
                    }
                }
             }
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText(listginfy.get(position));
        view.imgViewGinfy.setImageResource(listimage.get(position));

        return convertView;
    }

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


}
端木野
2023-03-14

替换

mAdapter = results.get(position);

                switch(mAdapter.getItemId())

switch(position)
华锦程
2023-03-14

你的代码有很多错误…(如android: clickable="True"在你的布局中

仅用于测试,请将代码替换为:

// Implement On Item click listener
        gridView.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {



            switch(position)
            {
            case 1:
            Intent newActivity = new Intent(GinfyActivity.this,MainActivity.class);
            startActivity(newActivity);
            break;

            case 2:
            Intent new1Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity.class);
            startActivity(new1Activity);
            break;
            default:
                Toast.makeText(GinfyActivity.this, "Wrong Input", Toast.LENGTH_LONG).show();
                }
        }
    });

适配器的getItemId总是返回0…

 类似资料:
  • 问题内容: 我整夜都在绞尽脑汁,但似乎无法完成这一小事情。我想将SwitchPreference添加到我的应用程序的PreferenceActivity中。下面是一张图片。 在我说太多之前,我的问题恰好是这样的:我似乎无法仅在首选项的Switch部分上放置一个侦听器。我可以在首选项上设置onPreferenceTreeClick和onPreferenceClick,如果按文本部分,效果很好。但是,

  • 我为Android网格视图设置了一个选择器,如下所示 当我单击gridview中的一个孩子时,它工作正常(背景正在设置),并且在gridview中失去焦点后背景没有消失。 但是当我手动选择一个子视图,如,它设置背景,但当它从gridview失去焦点时,它会消失。 顺便说一下,我的选择器是这样的: 我尝试过通过设置但没有用。

  • 我有一个简单的HibernateInterceptor,基本上我想自动设置几个字段。此拦截器(如下所示)扩展了EmptyInterceptor: 我使用spring配置文件进行布线,如下所示: 但是,永远无法到达拦截器。有人有什么线索吗?我还尝试将以下内容添加到事务管理器bean定义中,如下所示:

  • 下面的代码是项目的动作侦听器。基本上,我有 4 个单选按钮,当我单击一个时,我希望它更改屏幕上的变量。当我运行代码时,它只是将所有值相加。还有其他方法可以做到这一点吗?

  • 问题内容: 我正在尝试使用TableView组件的onScroll事件侦听器: FXML: Java控制器: 但这不起作用!我做错了什么? 我需要捕获垂直滚动器以手动控制滚动位置并根据向下或向上滚动获取相关数据。 谢谢大家! 问题答案: 我认为可能发生的情况是TableView包含的自身正在消耗scroll事件并在内部对其进行处理,因此它永远不会到达您的应用程序处理程序。 最初,我认为您可能要使用