在runnable中使用gridview会产生错误

时间:2014-05-31 17:21:18

标签: android gridview timer handler runnable

有人可以解释如何在GridView内使用Runnable

Handler starthandler = new Handler();
Runnable startrun= new Runnable(){
    @Override
    public void run() {
        G1=(GridView)findViewById(R.id.balloongrid);
        G1.setAdapter(new BalloonImageAdaper(this)); 
    }
};

上面的代码给出了以下错误:

  

构造函数Cacrballoonclass.BalloonImageAdaper(new Runnable(){})未定义

        public class BalloonImageAdaper extends BaseAdapter{ // create a class by name 
        imageadapter so as to fill the gridview with data

    private Context ballooncontext;


     public BalloonImageAdaper (Context c) {
            ballooncontext = c;
        }

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

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

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

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) { 
        // TODO Auto-generated method stub
        ImageView balloonimageview;

         if(arg1==null){
            balloonimageview = new ImageView(ballooncontext);
            balloonimageview.setLayoutParams(new GridView.LayoutParams(75,75));
            balloonimageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
            balloonimageview.setPadding(7, 7, 7, 7);
        }else {
                balloonimageview = (ImageView)arg1;
                }
        // lets inflate balloon randomly

     int rballoon = new Random().nextInt(ballooninstruct.length);   

              balloonimageview.setImageResource(balloonthumbs[rballoon]);            

               balloonimageview.setTag(balloonthumbs[rballoon]);

    return balloonimageview;
    }

}// class imageAdapter extends baseadapter

 @Override
    protected void onPause() {
    // TODO Auto-generated method stub
    super.onResume();
    finish();
} 

      }//class ends cacrballoonclasss

1 个答案:

答案 0 :(得分:0)

替换

new BalloonImageAdaper(this)

new BalloonImageAdaper(mContext) // mContext: reference to any Context (Activity or whatever)

(如果在Activity内,您可以使用NameOfActivity.this。)

this中的Runnable指的是您的Runnable,而不是像我认为您希望它引用您的Activity