我不确定标题是否合适,但我会尝试更好地解释它。
我想在布局中显示一个网格。为此,我使用自定义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吗?
答案 0 :(得分:4)
implement setOnItemClickListener for gridview item click:
@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
}
});
}
答案 1 :(得分:0)
您可以在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;
}