在gridview中添加switch case后,应用程序崩溃了

时间:2018-03-03 16:51:44

标签: java android gridview onclicklistener

当我尝试在自定义页面适配器中添加switch case时出现问题。如果点击了一个gridview项,我可以敬酒。但是当我尝试使用onClick添加新活动时,应用程序崩溃了。如果单击gridview项,我想显示新活动。我已经搜索过并研究过这个问题,但我找不到解决方案。我认为我的问题与切换案例有关。我正在学习android。请帮我。

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));
    }
}

1 个答案:

答案 0 :(得分:1)

这不是设置OnItemClick侦听器的方法。如果要设置单击整个项目,则应使用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;
            }

        } });

注意: - switchOnClickListenergetView()不需要collapse navbar-collapse,因为它一次只能调用一个位置。