I am new to Android and I'm trying to add setOnItemClickListener to my gridview and i am getting the following error:
setOnItemClickListener in AdapterView cannot by applied to MainActivity
for this code line: grid.setOnItemClickListener(this);
What am I doing wrong? Could you help me please?
Here is my code
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
GridView grid;
TextView tvTest;
TextView tvTest2;
Random rnd = new Random();
int color;
ImageView imageView;
ArrayList<Integer> colorInts = new ArrayList<Integer>();
ArrayList<Integer> itemsList = new ArrayList<Integer>();
Integer[] items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid = (GridView) findViewById(R.id.grid);
tvTest =(TextView) findViewById(R.id.tvTest);
tvTest2 =(TextView) findViewById(R.id.tvTest2);
adjustGridView();
itemsList.add(R.drawable.lemon);
itemsList.add(R.drawable.lemon);
itemsList.add(R.drawable.lemon);
itemsList.add(R.drawable.lemon);
itemsList.add(R.drawable.lemon);
itemsList.add(R.drawable.lemon);
// ArrayList into Array
items = new Integer[ itemsList.size() ];
itemsList.toArray( items );
grid.setAdapter(new CustomGridAdapter(this, items));
grid.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Clicked postion is " + arg2,
Toast.LENGTH_SHORT).show();
tvTest.setText("Number of color: "+ colorInts);
tvTest2.setText("Number of color: "+ colorInts.get(arg2));
}
// Here is your custom Adapter
public class CustomGridAdapter extends BaseAdapter {
private AppCompatActivity mContext;
// Keep all Images in array
public Integer[] mThumbIds;
// Constructor
public CustomGridAdapter(MainActivity mainActivity, Integer[] items) {
this.mContext = mainActivity;
this.mThumbIds = items;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
imageView.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
colorInts.add(color);
return imageView;
}
}
private void adjustGridView() {
grid.setNumColumns(3); //(GridView.AUTO_FIT);
grid.setColumnWidth(80);
grid.setVerticalSpacing(5);
grid.setHorizontalSpacing(5);
grid.setStretchMode(GridView.NO_STRETCH);
}
}
and here is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.testrk.basiclayout001.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/myColor02"
android:layout_weight="2"
>
<TextView
android:id="@+id/tvTest"
android:background="@color/myColor04"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/myColor01"
android:focusable="false"
android:layout_weight="1"
>
<GridView
android:id="@+id/grid"
android:focusable="true"
android:clickable="true"
android:background="@color/myColor03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
</GridView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/myColor02"
android:layout_weight="2"
>
<TextView
android:id="@+id/tvTest2"
android:background="@color/myColor04"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
and cell.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imgv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</ImageView>
The setOnItemClickListener method does not take in the context as an argument, it takes in an OnItemClickListener object. What you need to do is pass in a new OnItemClickListener in the method call and then override the OnItemClick method.
Example:
I hope this answers your question.