2011-08-22 44 views
2

我正在尝试将整数数组传递给baseadapter,以便A.class将整数数组传递给B.class中的BaseAdapter。这里是我想过去的整数数组的A.class(发件人):将整数传递给BaseAdapter

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

而且在我的活动B BaseAdapter:

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; 

} 
} 

现在显然上面的代码不正确,我不能用图像适配器中的getintent。但是这是我想要实现的一个说明性示例,并且想知道如果可能,如何通过意图将变量或数组传递给BaseAdapter。

回答

1

更新您的ImageAdapter的构造函数接受一个整数数组,像这样:

int[] mResources; 

public ImageAdapter(Context c, int[] resources) { 
    mResources = resources; 
    mContext = c; 
} 

然后在你的活动初始化适配器时,只需将它传递额外的int数组:

ImageAdapter adapter = new ImageAdapter(context, intArray); 
+0

真棒谢谢你!你刚刚救了一天 – benbeel

2

getIntent ()将在您的类B中提供。

您可以将Bundle或整数数组放入ImageAdapter的构造函数中

public class ImageAdapter extends BaseAdapter { 

    public ImageAdapter(Context c, Bundle b) { 
     int[] mThumb = b.getIntArray("mThumbSent"); 
     mContext = c; 
    } 

    ..... 
} 

在你的活动B构建你的适配器是这样的:

ImageAdapter adapter = new ImageAdapter(yourcontext,getIntent().getExtras()); 

要使用您的适配器在哪里?在ListView中?然后,继承ListAdapter而不是BaseAdapter就足够了。