在gridview中添加switch case后,应用程序崩溃了
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在gridview中添加switch case后,应用程序崩溃了相关的知识,希望对你有一定的参考价值。
当我尝试在自定义页面适配器中添加switch case时,我遇到了问题。如果点击了一个gridview项,我可以敬酒。但是当我尝试使用onClick添加新活动时,应用程序崩溃了。如果单击gridview项,我想显示新活动。我已经搜索过并研究过这个问题,但我找不到解决方案。我认为我的问题与切换案例有关。我正在学习android。请帮我。
custom adapter.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;
}
}
main activity.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
Listener的方法。如果你想设置点击整个项目,你应该使用OnItemClickListener
。从OnClickListener
删除getView()
并使用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;
}
} });
注意: - switch
内部不需要OnClickListener
,因为它只能在一次调用一个位置。
以上是关于在gridview中添加switch case后,应用程序崩溃了的主要内容,如果未能解决你的问题,请参考以下文章
SWITCH语句中如果执行了一条CASE语句是空的后如果没有BREAK会不会执行DEFAULT语句(其他CASE也没BREAK)
android gridview 如何点击item中的一个组合控件的图标按钮触发整个item