У меня мало проблем, как я узнаю, какой элемент был нажат с помощью OnClickEvent. Я рисую сетчатое меню с помощью изображений + текст. Вот мой код:Получить тот элемент, который был нажат
public class MyActivity extends Activity implements OnItemClickListener {
GridView menu;
private String[] menu_text = {
"Menu1",
"Menu2",
"Menu3",
"Menu4",
"Menu5",
"Menu6",
"Menu7",
"Menu8"};
private Integer[] menu_icon = {
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,};
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
menu = (GridView)findViewById(R.id.Menu);
menu.setOnItemClickListener(this);
menu.setAdapter(new MenuItem(this, R.layout.menu_item, menu_text));
}
public class MenuItem extends ArrayAdapter {
public MenuItem(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.menu_item, parent, false);
TextView tv=(TextView)row.findViewById(R.id.text);
tv.setText(menu_text[position]);
tv.setTextColor(Color.BLACK);
tv.setCompoundDrawablesWithIntrinsicBounds(0, menu_icon[position], 0, 0);
return row;
}
}
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
// TODO Action to perform
}
}
Так что мой вопрос, как я могу распознать, какой пункт был щелкнул в этом меню сетки.
общественного недействительный onItemClick (AdapterView arg0, Просмотр arg1, внутр арг2, длинные arg3) { Строка menuText = menu_text [arg2]; Drawable d = getResources(). GetDrawable (menu_icon [arg2]); } –