Gridview onclick监听器无法正常工作

时间:2013-07-01 10:02:49

标签: java android android-gridview

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

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;
    }


}

Mainactivity.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 个答案:

答案 0 :(得分:1)

您的代码中有很多错误..(例如布局中的android:clickable="True"&gt;&gt;删除)

仅用于测试,用以下代码替换您的代码:

// 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 ..

答案 1 :(得分:0)

替换

mAdapter = results.get(position);

                switch(mAdapter.getItemId())

通过

switch(position)

答案 2 :(得分:0)

我一直在使用这样的东西。在你的情况下,这应该工作。

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;
    }


}