我试图将一个整数数组传递给baseadapter,这样A.class会将一个整数数组传递给B.class中的BaseAdapter。以下是我在A.Class(发送者)中传递整数数组的方法:
int [] mThumb = {
R.drawable.image1_thumb, R.drawable.image2_thumb, R.drawable.image3_thumb,
R.drawable.image4_thumb, R.drawable.image5_thumb, R.drawable.image6_thumb,
R.drawable.image7_thumb, R.drawable.image8_thumb, R.drawable.image9_thumb,
R.drawable.image10_thumb};
Bundle b=new Bundle();
b.putIntArray("mThumbSent", mThumb);
Intent startSwitcher = new Intent(A.this, B.class);
startSwitcher.putExtras(b);
在我的BaseAdapter活动B中:
public class ImageSwitch1 extends Activity Extends ...{
onCreate....
[redacted]
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
Bundle b=this.getIntent().getExtras();
int[] mThumb = b.getIntArray("mThumbSent");
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumb.length; //this used to say return mThumbIds
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView im = new ImageView(mContext);
im.setImageResource(mThumb[position]);
im.setAdjustViewBounds(true);
im.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
im.setBackgroundResource(R.drawable.picture_frame);
return im;
}
private Context mContext;
}
}
现在显然上面的代码不正确,我不能在imageadapter中使用getintent。但这是我想要完成的一个说明性示例,并且如果可能的话,想知道如何通过意图将变量或数组传递给BaseAdapter。
答案 0 :(得分:2)
getIntent()将在您的B类中提供。
您可以将Bundle或整数数组放入ImageAdapter
的构造函数中public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c, Bundle b) {
int[] mThumb = b.getIntArray("mThumbSent");
mContext = c;
}
.....
}
在Activity B中以这种方式构建适配器:
ImageAdapter adapter = new ImageAdapter(yourcontext,getIntent().getExtras());
您想在哪里使用适配器?在ListView?然后将ListAdapter子类化为BaseAdapter就足够了。
答案 1 :(得分:1)
更新ImageAdapter的构造函数以接受如下所示的整数数组:
int[] mResources;
public ImageAdapter(Context c, int[] resources) {
mResources = resources;
mContext = c;
}
然后在Activity中初始化Adapter时,只需传递额外的int数组:
ImageAdapter adapter = new ImageAdapter(context, intArray);