gpt4 book ai didi

java - Gridview onclick 监听器不工作

转载 作者:行者123 更新时间:2023-12-01 14:22:51 24 4
gpt4 key购买 nike

我正在使用布局的gridview,它在列表中的 GridView 项目之后不起作用,它在logcat错误中显示“throwIndexOutOfBoundsException”。如果我使用toast其工作,则没有onclick函数,如果我为下一个 Activity 提供onlcick函数它不工作

Gridviewadapter.java

public class GridviewAdapter extends BaseAdapter
{
private ArrayList<String> listginfy;
private ArrayList<Integer> listimage;
private Activity activity;

public GridviewAdapter(GinfyActivity ginfyActivity,ArrayList<String> listginfy, ArrayList<Integer> listimage) {
super();
this.listginfy = listginfy;
this.listimage = listimage;
this.activity = (Activity) ginfyActivity;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return listginfy.size();
}

@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listginfy.get(position);
}

public int getItemId() {
// TODO Auto-generated method stub
return 0;
}

public static class ViewHolder
{
public ImageView imgViewGinfy;
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_row, null);

view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
view.imgViewGinfy = (ImageView) convertView.findViewById(R.id.imageView1);

convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}

view.txtViewTitle.setText(listginfy.get(position));
view.imgViewGinfy.setImageResource(listimage.get(position));

return convertView;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}


}

Mainactivity.java

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_ginfy);

prepareList();

// prepared arraylist and passed it to the Adapter class
mAdapter = new GridviewAdapter(this,listginfy, listimage);

// Set custom adapter to gridview
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(mAdapter);

// Implement On Item click listener
gridView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {


mAdapter = results.get(position);

switch(mAdapter.getItemId())
{
case 1:
Intent newActivity = new Intent(GinfyActivity.this,MainActivity.class);
startActivity(newActivity);
break;

case 2:
Intent new1Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity.class);
startActivity(new1Activity);
break;
default:
Toast.makeText(GinfyActivity.this, "Wrong Input", Toast.LENGTH_LONG).show();
}
}
});

}

public void prepareList()
{
listginfy = new ArrayList<String>();

listginfy.add("Prayers");
listginfy.add("Poojaroom");


listimage = new ArrayList<Integer>();
listimage.add(R.drawable.ginfyprayer);
listimage.add(R.drawable.poojaroom1);

}


}

gridview的onclick功能不起作用在这里我也发布了我的 xml 布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/border"
android:padding="5dp">

<ImageView
android:layout_height="64dp"
android:id="@+id/imageView1"
android:layout_width="64dp"
android:src="@drawable/icon"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:clickable="True">
</ImageView>

<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:ellipsize="marquee"></TextView>


</RelativeLayout>

home_ginfy.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<GridView
android:layout_height="wrap_content"
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:clickable="True">
</GridView>
</LinearLayout>

最佳答案

您的代码中有很多错误..(例如布局中的 android:clickable="True" >> 删除它)

仅供测试,请将代码替换为:

// Implement On Item click listener
gridView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {



switch(position)
{
case 1:
Intent newActivity = new Intent(GinfyActivity.this,MainActivity.class);
startActivity(newActivity);
break;

case 2:
Intent new1Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity.class);
startActivity(new1Activity);
break;
default:
Toast.makeText(GinfyActivity.this, "Wrong Input", Toast.LENGTH_LONG).show();
}
}
});

适配器的 getItemId 始终返回 0..

关于java - Gridview onclick 监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17401778/

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