有人可以解释如何在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
答案 0 :(得分:0)
替换
new BalloonImageAdaper(this)
带
new BalloonImageAdaper(mContext) // mContext: reference to any Context (Activity or whatever)
(如果在Activity
内,您可以使用NameOfActivity.this
。)
this
中的Runnable
指的是您的Runnable
,而不是像我认为您希望它引用您的Activity
。