Android myActivity扩展GridView实现OnClickListener

Android myActivity扩展GridView实现OnClickListener,android,android-layout,gridview,onclicklistener,Android,Android Layout,Gridview,Onclicklistener,我不确定标题是否合适,但我会尽力更好地解释 我想在布局中显示网格。为此,我使用了一个定制的ArrayAdapter。 我不知道如何为每个项设置onClickListener 布局包含用于显示名称的标签和gridview 这是我的活动: public class Sala extends Activity{ GridView gridView; TextView label_name; ArrayList<String> lista_opciones = new ArrayList&

我不确定标题是否合适,但我会尽力更好地解释

我想在布局中显示网格。为此,我使用了一个定制的ArrayAdapter。 我不知道如何为每个项设置onClickListener

布局包含用于显示名称的标签和gridview

这是我的活动:

public class Sala extends Activity{
GridView gridView;
TextView label_name;

ArrayList<String> lista_opciones = new ArrayList<String>();  

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_sala);
    //Refresh the name in the label
    Bundle bundle=getIntent().getExtras();
    String name= bundle.getString("name");
    label_name= (TextView) findViewById(R.id.label_name);
    label_name.setText(name);

    //Loading array
    lista_op.add("Op 1");  
    lista_op.add("Op 2");  
    lista_op.add("Op 3");  
    lista_op.add("Op 4"); 

    //AdaptadorOpciones
    ArrayAdapterOpciones mAdapter = new ArrayAdapterOptions(this,lista_op);  

    //Set the customAdapter to the grid
    gridView = (GridView) findViewById(R.id.grid); 
    gridView.setAdapter(mAdapter);  
}
}


我应该重写哪些方法以在每个项目中实现onClickListener?

implement setOnItemClickListener for gridview项目单击:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_sala);
    //Refresh the name in the label
    Bundle bundle=getIntent().getExtras();
    String name= bundle.getString("name");
    label_name= (TextView) findViewById(R.id.label_name);
    label_name.setText(name);

    //Loading array
    lista_op.add("Op 1");  
    lista_op.add("Op 2");  
    lista_op.add("Op 3");  
    lista_op.add("Op 4"); 

    //AdaptadorOpciones
    ArrayAdapterOpciones mAdapter = new ArrayAdapterOptions(this,lista_op);  

    //Set the customAdapter to the grid
    gridView = (GridView) findViewById(R.id.grid); 
    gridView.setAdapter(mAdapter);  
    gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                //Do stuff here
            }
        });
}

gridview项目的Implement setOnItemClickListener单击:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_sala);
    //Refresh the name in the label
    Bundle bundle=getIntent().getExtras();
    String name= bundle.getString("name");
    label_name= (TextView) findViewById(R.id.label_name);
    label_name.setText(name);

    //Loading array
    lista_op.add("Op 1");  
    lista_op.add("Op 2");  
    lista_op.add("Op 3");  
    lista_op.add("Op 4"); 

    //AdaptadorOpciones
    ArrayAdapterOpciones mAdapter = new ArrayAdapterOptions(this,lista_op);  

    //Set the customAdapter to the grid
    gridView = (GridView) findViewById(R.id.grid); 
    gridView.setAdapter(mAdapter);  
    gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                //Do stuff here
            }
        });
}

您可以在Adapter.getView实现中添加侦听器:

    @Override       
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView button = (ImageView) convertView;
        if (button == null) {
            button = new ImageButton(); // or whatever
            button.setOnClickListener(this);
        }
        button.setId(ids_present.get(position));
        return button;
    }

您可以在Adapter.getView实现中添加侦听器:

    @Override       
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView button = (ImageView) convertView;
        if (button == null) {
            button = new ImageButton(); // or whatever
            button.setOnClickListener(this);
        }
        button.setId(ids_present.get(position));
        return button;
    }

我已经试过了,它可以编译,但从来没有遇到inItemClick方法。如果控件没有出现在item click listener上,那么可能是您在布局中将clickable设置为true。从布局的所有视图中删除clickable,然后重试。我已经检查了它,但在我的布局适配器中没有任何clickable属性设置为true,在布局适配器中也没有设置为true。主要问题已解决:每个项目都是一个按钮clickable没有设置为true,而是一个按钮。我用TextView代替了它,现在它工作得很好。我试过了,它可以编译,但从来没有运行到inItemClick方法。如果控件没有出现在item click listener上,那么可能是您在布局中将clickable设置为true。从布局的所有视图中删除clickable,然后重试。我已经检查了它,但在我的布局适配器中没有任何clickable属性设置为true,在布局适配器中也没有设置为true。主要问题已解决:每个项目都是一个按钮clickable没有设置为true,而是一个按钮。我放了一个文本视图而不是它,现在它工作得很好。