gpt4 book ai didi

java - 在 gridview 中添加 switch case 后应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 11:34:00 27 4
gpt4 key购买 nike

当我尝试在自定义页面适配器中添加 switch case 时遇到问题。如果单击 gridview 的一项,我可以举杯。但是当我尝试使用 onClick 添加新 Activity 时,应用程序崩溃了。如果单击 gridview 项目,我想显示新 Activity 。我已经搜索和研究过这个问题,但找不到解决方案。我认为我的问题与 switch case 有关。我正在学习安卓。请帮我。

CustomAdapter.java

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapter extends BaseAdapter{

String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] osNameList, int[] osImages) {
// TODO Auto-generated constructor stub
result=osNameList;
context=mainActivity;
imageId=osImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

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

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

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

public class Holder
{
TextView os_text;
ImageView os_img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;

rowView = inflater.inflate(R.layout.sample_gridlayout, null);
holder.os_text =(TextView) rowView.findViewById(R.id.os_texts);
holder.os_img =(ImageView) rowView.findViewById(R.id.os_images);

holder.os_text.setText(result[position]);
holder.os_img.setImageResource(imageId[position]);

rowView.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
switch(position){
case 0:
Intent a = new Intent(v.getContext(), FirstActivity.class);
v.getContext().startActivity(a);
break;
case 1:
Intent b = new Intent(v.getContext(), DefaultActivity.class);
v.getContext().startActivity(b);
break;

}


} });

return rowView;
}



}

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;

public class MainActivity extends Activity {

GridView gridview;

public static String[] osNameList = {
"Android",
"iOS",
"Linux",
};
public static int[] osImages = {
R.drawable.alpha,
R.drawable.beta,
R.drawable.cupcake,
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.customgrid);
gridview.setAdapter(new CustomAdapter(this, osNameList, osImages));
}
}

最佳答案

这不是设置OnItemClick监听器的方法。如果你想设置对整个项目的点击,你应该使用OnItemClickListener。从 getView() 中删除 OnClickListener 并使用 OnItemClickListener,如下所示。

 GridView gridview = (GridView) findViewById(R.id.customgrid);
gridview.setAdapter(new CustomAdapter(this, osNameList, osImages));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
Intent a = new Intent(v.getContext(), FirstActivity.class);
startActivity(a);
break;
case 1:
Intent b = new Intent(v.getContext(), DefaultActivity.class);
startActivity(b);
break;
default:
break;
}

} });

注意:- 在 getView() 内部的 OnClickListener 中不需要 switch,因为它的一次只会要求一个职位。

关于java - 在 gridview 中添加 switch case 后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086689/

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