如何为2个导致错误结果的网格视图修复setOnItemClickListener?

时间:2014-09-12 06:36:39

标签: android gridview onitemclicklistener

这是我的代码

// 1st gridview 
        prepareList1();
        mAdapter = new GridView_Adapter(this,listAccName, listAccIcon);
        gridView1 = (GridView) findViewById(R.id.gridview1;
        gridView1.setAdapter(mAdapter);
        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                    long arg3) {
                Toast.makeText(Main.this,"position = " + position + ":"+ mAdapter.getItem(position) , Toast.LENGTH_SHORT).show();
            }
        });

    // 2nd gridview
    prepareList2();
        mAdapter = new GridView_Adapter(this,listCatName, listCatIcon);
        gridView2 = (GridView) findViewById(R.id.gridview2);
        gridView2.setAdapter(mAdapter);
        gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                    long arg3) {
                Toast.makeText(Main.this,"position = " + position + ":"+ mAdapter.getItem(position) , Toast.LENGTH_SHORT).show();
            }
        });

但是当我点击第一个gridview项时,它会在Toast上显示第二个gridview变量。我尝试制作差异变量名,但结果是一样的。

谢谢

1 个答案:

答案 0 :(得分:2)

   GridView_Adapter  mAdapter1 = new GridView_Adapter(this,listCatName, listCatIcon);
            gridView2 = (GridView) findViewById(R.id.gridview2);
            gridView2.setAdapter(mAdapter1);

只需改变这三行