0

我在网格视图中链接活动时遇到问题。

我的代码在这里:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }
    public int getCount() {
        return mThumbIds.length;
    }
    public Object getItem(int position) {
        return null;
    }
    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    @SuppressLint("NewApi")
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setPadding(5, 5, 5, 5);
            imageView.setBackgroundColor(0);
            imageView.setAdjustViewBounds(true);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbIds[position]);
        if(position == 2)
        {
           Intent nextScreen = new Intent(getApplicationContext(), News.class);
           startActivity(nextScreen);   
        }
        if(position == 1)
        {
           Intent nextScreen = new Intent(getApplicationContext(), Open.class);
           startActivity(nextScreen);   

        }

        return imageView;

    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.info, R.drawable.open,
            R.drawable.news, R.drawable.specials,
            R.drawable.maerkte, R.drawable.anfahrt,
            R.drawable.artikel, R.drawable.kontakt,

    };

}

我认为这只是一个小问题:(我几天以来一直在寻找答案,.. :(我希望你能帮助我,在我得到正确答案之前我会感谢你;)

祝你今天过得愉快

4

1 回答 1

0

把代码放在你的onItemClickListener(),而不是getView()

就像是:

gridView.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        if (pos == 2) {
           Intent nextScreen = new Intent(getApplicationContext(), News.class);
           startActivity(nextScreen);   
        }
        else if (pos == 1) {
           Intent nextScreen = new Intent(getApplicationContext(), Open.class);
           startActivity(nextScreen);   
        }
    }
});

此代码将在您的 Activity/Fragment/任何您拥有 gridView 的地方,而不是在您的自定义适配器中。

于 2013-06-26T15:32:54.367 回答