Skip to content Skip to sidebar Skip to footer

Gridview Onclick Listener Its Not Working

I am using gridview of layout,its not working after the grid view items in the list,it shows 'throwIndexOutOfBoundsException' in logcat error.without onclick function if i use toas

Solution 1:

you have a lot of error in your code.. (like android:clickable="True" in your layout >> remove that)

just for test, replace your code with:

// Implement On Item click listener
        gridView.setOnItemClickListener(newOnItemClickListener() 
        {
            @OverridepublicvoidonItemClick(AdapterView<?> a, View v, int position, long id) {



            switch(position)
            {
            case1:
            IntentnewActivity=newIntent(GinfyActivity.this,MainActivity.class);
            startActivity(newActivity);
            break;

            case2:
            Intentnew1Activity=newIntent(GinfyActivity.this,AndroidTabLayoutActivity.class);
            startActivity(new1Activity);
            break;
            default:
                Toast.makeText(GinfyActivity.this, "Wrong Input", Toast.LENGTH_LONG).show();
                }
        }
    });

The getItemId for your adapter always return 0..

Solution 2:

replace

mAdapter = results.get(position);

                switch(mAdapter.getItemId())

by

switch(position)

Solution 3:

I have been using something like this. In your case this should work.

publicclassGridviewAdapterextendsBaseAdapter
{
    private ArrayList<String> listginfy;
    private ArrayList<Integer> listimage;
    private Activity activity;

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

    @OverridepublicintgetCount() {
        // TODO Auto-generated method stubreturn listginfy.size();
    }

    @Overridepublic String getItem(int position) {
        // TODO Auto-generated method stubreturn listginfy.get(position);
    }

    publicintgetItemId() {
        // TODO Auto-generated method stubreturn0;
    }

    publicstaticclassViewHolder
    {
        public ImageView imgViewGinfy;
        public TextView txtViewTitle;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflaterinflator= activity.getLayoutInflater();

        if(convertView==null)
        {
            view = newViewHolder();
            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(newOnClickListener() {

                @OverridepublicvoidonClick(View v) {
                    // TODO Auto-generated method stubswitch (position){
                         case1:
                              IntentnewActivity=newIntent(GinfyActivity.this,MainActivity.class);
                              startActivity(newActivity);
                              break;
                         case2:
                              Intentnew1Activity=newIntent(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;
    }

    @OverridepubliclonggetItemId(int arg0) {
        // TODO Auto-generated method stubreturn0;
    }


}

Post a Comment for "Gridview Onclick Listener Its Not Working"