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,

    };

}

小さな問題だと思います:(答えを得るために数日以来探しています..:(あなたが私を助けてくれることを願っています.正しい答えを得る前に感謝します;)

良い1日を

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

このコードは、カスタムアダプター内ではなく、アクティビティ/フラグメント/gridView がある場所にあります。

于 2013-06-26T15:32:54.367 に答える