问题描述
,我已经使用的ImageButton和网格视图文本视图,但我不能setOnItemClickListener对这些项目(Imean空话发生在点击)
我的code是
这是我的适配器的GridView
I am using GridView for displaying the categories , i have used ImageButton and a Text View in Grid View but I am not able to setOnItemClickListener on those items( Imean nothings Happening on Clicking it)My code isthis is My Adapter for GridView
public class MyAdapter extends BaseAdapter{
private ArrayList<String> listJewel_name;
private ArrayList<Integer> listJewellery;
private Activity activity;
//private LayoutInflater inflater;
public MyAdapter(Activity activity,ArrayList<String> listJewel_name, ArrayList<Integer> listJewellery) {
super();
this.listJewel_name = listJewel_name;
this.listJewellery = listJewellery;
this.activity = activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listJewel_name.size();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listJewel_name.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public ImageButton imgViewJewel;
public TextView txtViewTitle;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridview, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.tvtext);
view.imgViewJewel = (ImageButton) convertView.findViewById(R.id.picture);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewTitle.setText(listJewel_name.get(position));
view.imgViewJewel.setImageResource(listJewellery.get(position));
return convertView;
}
}
我activity.class是
My activity.class is
GridView gridView;
Button add;
private MyAdapter mAdapter;
private ArrayList<String> listJewel_name;
private ArrayList<Integer> listJewellery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_jewellery_option);
prepareList();
// prepared arraylist and passed it to the Adapter class
mAdapter = new MyAdapter(this,listJewel_name, listJewellery);
// Set custom adapter to gridview
gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(mAdapter);
// Implement On Item click listener
gridView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
String shri=mAdapter.getItem(position);
Toast.makeText(SelectJewelleryOption.this, shri , Toast.LENGTH_SHORT).show();
}
});
}
我的XML文件是
My XML file is
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.myjewelbox.SquareImageButtonView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="130dp"
android:scaleType="fitCenter"
android:clickable="true" />
<TextView
android:id="@+id/tvtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="1dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_gravity="bottom"
android:textColor="@color/Golden"
android:background="#55000000"
android:focusable="false"
/>
</FrameLayout>
我也在此XML文件到我的主XML文件的GridView控件
And I have set this XMl file to my Main XML file's GridView
推荐答案
设置为ItemClickListener在它的适配器,而不是直接的GridView设置ItemClickListener。GridView控件的项目
Set ItemClickListener for items of GridView in it's adapter,instead of set an ItemClickListener directly for GridView.
view.imgViewJewel.setItemClickListener(活动);
view.imgViewJewel.setItemClickListener(activity);
这篇关于没有任何事情发生在GridView中单击项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!