我不能在switch语句中使用Integer []数组吗?

时间:2015-02-14 06:24:54

标签: android arrays android-intent switch-statement

我认为Integer[]数组可以在switch语句中使用,因为自动装箱,Integer对象和int原语来回交换由编译器自动完成。但我没有把我的Integer[]项目放入OnClick事件的switch语句中。当用户单击我的数组中的图像时,我希望它在新活动中打开。我究竟做错了什么?我的代码在case号和startActivity(i)都出现红色错误,所以这肯定是不对的。感谢。

GridViewSitesAdapter.java

package org.azurespot.cutelinks;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import org.azurespot.R;

/**
 * Created by mizu on 2/11/15.
 */
public class GridViewSitesAdapter extends BaseAdapter {

    public Context mContext;

    public GridViewSitesAdapter(Context c) {
        mContext = c;
    }

    // Keep all Images in array
    public Integer[] mThumbIds = new Integer[]{
            R.drawable.cute_overload, R.drawable.attack_of_the_cute,
            R.drawable.zoo_borns, R.drawable.cutest_paw,
            R.drawable.mochimochiland, R.drawable.baby_mugging,
            R.drawable.cutest_food, R.drawable.tiny_cute_things,
            R.drawable.etsy_robot_plush,
            R.drawable.fuck_yeah_fruits,
            R.drawable.ark_in_space,
            R.drawable.we_heart_it
    };

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams
                (270,270));
        imageView.setBackgroundResource(R.drawable.gridview_image_border);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (mThumbIds){
                case 0:
                    Intent i = new Intent(mContext, CuteOverloadActivity.class);
                    startActivity(i);
                    break;
                case 1:

            }

                }
            }
        });
        return imageView;
    }
}

2 个答案:

答案 0 :(得分:0)

使用setTag/getTag方法获取点击行的实际位置:

    imageView.setTag(position);
    imageView.setOnClickListener(new View.OnClickListener() {
          @Override
           public void onClick(View v) {
                 int clickedPos=Integer.parseInt(v.getTag().toString());
                    switch (clickedPos){
                    case 0:
                      ....
                }
          }
   }
 });

答案 1 :(得分:0)

您的switch语句不应该在mThumbIds上,而应在position上(因为它是确定要执行的操作的元素的位置)。请注意,您需要将position声明为final才能在onClick方法中对其进行访问。