Android – 自定义BaseAdapter Gridview,创buildonclicklistener

我创build了一个显示字母表的字母的gridview。 我使用自定义BaseAdapter填充string数组的GridView。

我想要做的是能够获得点击的单元格的值(字母)。 为了validation它的工作原理,我创build了一个TextView,当用户点击一个项目(单元格)时,我想要设置TextView的文本与所选单元格的值

在这里输入图像说明

我做了一个不行的尝试。 这是我的代码:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" tools:ignore="MergeRootFrame" > <GridView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/myGridView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:numColumns="7" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/feedback" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> </RelativeLayout> 

cell.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/grid_item" android:layout_gravity="center" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="05" android:textSize="20sp" android:enabled="true" android:clickable="true"> </Button> </LinearLayout> 

主要活动

 public class MainActivity extends Activity { private TextView text; private GridView gridView; private final String[] items = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.feedback); gridView = (GridView) this.findViewById(R.id.myGridView); CustomGridAdapter gridAdapter = new CustomGridAdapter(MainActivity.this, items); gridView.setAdapter(gridAdapter); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { text.setText((String) (gridView.getItemAtPosition(position))); Log.i("ITEM_CLICKED", "" + (String) (gridView.getItemAtPosition(position))); } }); } } 

CustomGridAdapter

 public class CustomGridAdapter extends BaseAdapter { private Context context; private String[] items; LayoutInflater inflater; public CustomGridAdapter(Context context, String[] items) { this.context = context; this.items = items; inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.cell, null); } Button button = (Button) convertView.findViewById(R.id.grid_item); button.setText(items[position]); return convertView; } @Override public int getCount() { return items.length; } @Override public Object getItem(int position) { return items[position]; } @Override public long getItemId(int position) { return position; } } 

我究竟做错了什么?

编辑 :看来,gridView.setOnItemClickListener(…); 不被调用/执行。 我已经改变它为了检查它,我看不到logcat。

 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.i("ON_ITEM_CLICK_LISTENER", "item clicked"); } }); 

Solutions Collecting From Web of "Android – 自定义BaseAdapter Gridview,创buildonclicklistener"

在你的情况的问题是,该button正在消耗点击,并没有得到GridView。 我看到两个选项:

  1. 简单的一点:在cell.xml有你的Button这些属性: android:clickable="false" and android:focusable="false"
  2. grosso-modo:将Activity作为参数发送到适配器,并从MainActivity公开方法。 如果您想重新使用适配器,请发送抽象types的活动或接口。

如:

 public class MainActivity extends Activity { private TextView text; private GridView gridView; private final String[] items = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.feedback); gridView = (GridView) this.findViewById(R.id.myGridView); CustomGridAdapter gridAdapter = new CustomGridAdapter(MainActivity.this, items); gridView.setAdapter(gridAdapter); } public void itemClicked(int position) { text.setText(items[position]); } } 

并在您的适配器中:

 public class CustomGridAdapter extends BaseAdapter { private MainActivity context; private String[] items; LayoutInflater inflater; public CustomGridAdapter(MainActivity context, String[] items) { this.context = context; this.items = items; inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public View getView(final int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.cell, null); } Button button = (Button) convertView.findViewById(R.id.grid_item); button.setText(items[position]); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { context.itemClicked(position); } }); return convertView; } @Override public int getCount() { return items.length; } @Override public Object getItem(int position) { return items[position]; } @Override public long getItemId(int position) { return position; } } 

其实对于gridview的setOnItemClickListener你没有做错任何事情。 这是造成问题的单元格视图。 尝试更改单元格视图button到一个文本视图,它应该工作。 现在,当你点击gridview中的一个项目时,这个项目实际上并没有被点击,因为你的cell-viewbutton有自己的OnClickListener实现,这就是为什么setOnItemClickListener没有被调用。

你可以使用setTag()和getTag()来让你放置/获取任何types的对象。 所以在将来,你将能够通过任何事情(如果你做了适当的演员)

 public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.cell, null); } Button button = (Button) convertView.findViewById(R.id.grid_item); button.setText(items[position]); convertView.setTag(items[position]); return convertView; } 

 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String str = (String) view.getTag(); text.setText(str); Log.i("ITEM_CLICKED", "" + (String) (gridView.getItemAtPosition(position))); } });