Android:使用网格视图启动新活动。

时间:2012-02-06 11:56:27

标签: android android-layout

我已使用BaseAdapter进行网格视图,getView方法BaseAdapter我正在使用主要活动的上下文开始新活动,但是当我点击图标时,我收到消息应用程序发现1秒,然后新活动开始,在log cat中我收到错误PakageManager$PakageNotFoundEception。在此先感谢您的帮助。

public class ImageAdapter extends BaseAdapter{


    private Context mContext;
    ArrayList<Integer> imgid;
    Uri uri;

    public ImageAdapter(Context c, ArrayList<Integer> imgid2 ) {
        super();
        mContext = c;
        imgid=imgid2;

    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imgid.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
               // imageView.setClickable(true);
                imageView.setPadding(8, 8, 8, 8);


                imageView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                      Log.d("onClick","position ["+position+"]");

                      try {

                          final Intent intent= new Intent("settings");
                          intent.setClass(mContext, listAvtivity.class); //*******
                          if(imgid.get(position)==R.drawable.setting4){mContext.startActivity(intent);}


                          if(imgid.get(position)==R.drawable.google){uri = Uri.parse("http://www.google.com");}

                          if(imgid.get(position)==R.drawable.yahoo){uri = Uri.parse("http://www.yahoo.com");}
                          if(imgid.get(position)==R.drawable.skype){uri = Uri.parse("http://www.skype.com");}


                          Intent intent1 = new Intent(Intent.ACTION_VIEW, uri);
                          mContext.startActivity(intent1);


                    } catch (Exception e) {
                        // TODO: handle exception
                          Log.e("onClick Error: ",e.toString());

                    }
                 }

                  });

            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(imgid.get(position));
            return imageView;
    }

}

1 个答案:

答案 0 :(得分:1)

不要在getView方法中使用View.OnClickListener。使用OnItemClickListener并将其添加到GridView,以便在用户点击某个项目时收到通知。