Karthick M Karthick M - 3 months ago 15
Android Question

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 toast its working,if i give an onlcick function for next activity its not working

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

}


}


Onclick function of gridview is not working
here i post my xml layout also.

<?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>

Answer

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

The getItemId for your adapter always return 0..