android - 尝试将setonitemclicklistener添加到gridview并出错

时间:2017-01-09 09:02:26

标签: android gridview adapter onitemclicklistener

我是Android新手,我尝试将setOnItemClickListener添加到gridview,我收到以下错误:

  AdapterView中的

setOnItemClickListener无法应用于MainActivity

代码:grid.setOnItemClickListener(this);

我做错了什么?你能帮帮我吗?

这是我的代码

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

    }

}

这里是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>

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

1 个答案:

答案 0 :(得分:0)

setOnItemClickListener方法不接受上下文作为参数,它接受OnItemClickListener对象。您需要做的是在方法调用中传入一个新的OnItemClickListener,然后重写OnItemClick方法。

示例:

grid.setOnItemClickListener(new OnItemClickListener(){
  @Override
  public void onItemClick(AdapterView<?> adapter, View v, int position,
        long arg3) 
  {
       //code to be executed on click
  }

我希望这能回答你的问题。