将actionListener赋予gridview中的按钮

时间:2018-07-05 15:17:27

标签: java android gridview

我做了一个gridview并添加了一个ImageButton 如何使此按钮为每个项目提供不同的结果

这是onCreate方法中的代码

 GridView gridView = findViewById(R.id.gridview);
        ImageButton imageButton = findViewById(R.id.imageButton);
        ArrayList<info> itme = new ArrayList<>();

                    for (int i =0;i<9;i++)
                    {
                        itme.add(new info(names[i],size[i],pic[i]));
                    }

        BooksAdapter booksAdapter = new BooksAdapter(itme);
        gridView.setAdapter(booksAdapter);

和此类BaseAdapter

class BooksAdapter extends BaseAdapter {
    ArrayList<info>itme;

    BooksAdapter(ArrayList<info> itme) {
        this.itme = itme;
    }

    @Override
    public int getCount() {
        return itme.size();
    }
    @Override
    public Object getItem(int i) {
        return itme.get(i).text;
    }
    @Override
    public long getItemId(int i) {
        return i;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        @SuppressLint({"ViewHolder", "InflateParams"}) View view1 = getLayoutInflater().inflate(R.layout.grid,null);
        TextView textView = view1.findViewById(R.id.textView2);
        TextView textView1 = view1.findViewById(R.id.textView4);

        ImageView imageView = view1.findViewById(R.id.imageView2);

        textView.setText(itme.get(i).text);
        textView1.setText(itme.get(i).text1);
        imageView.setImageResource(itme.get(i).img);


        return view1;
    }
}

0 个答案:

没有答案