gpt4 book ai didi

java - 单击 GridView 的项目时没有任何反应

转载 作者:行者123 更新时间:2023-12-01 13:49:12 25 4
gpt4 key购买 nike

我正在使用 GridView 来显示类别,我在 GridView 中使用了 ImageButton 和 TextView ,但我无法在这些项目上设置 OnItemClickListener(我的意思是单击它时没有发生任何事情)我的代码是这是我的 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是

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 文件是

<?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

最佳答案

在其适配器中为 GridView 的项设置 ItemClickListener,而不是直接为 GridView 设置 ItemClickListener。

view.imgViewJewel.setItemClickListener(activity);

关于java - 单击 GridView 的项目时没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20096558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com