从适配器返回动态指定数量的图像

时间:2013-06-03 07:56:52

标签: android arrays image gridview baseadapter

我正在使用带有GridView的自定义ImageAdapter来显示4 * 4组图像。 我希望能够动态传递适配器返回的图像数量,到目前为止我所看到的教程/问题都是基于预设数量的图像,例如只返回前30个。我不确定如何实现一个接受int的方法并从数组中返回许多图像。任何帮助,将不胜感激。非常感谢

这是我到目前为止所有图像都返回到网格视图。

ImageAdapter

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;    
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter{
    private Context mContext;

// Keep all Images in array
public Integer[] mMona = {
        R.drawable.joconde_1_1, R.drawable.joconde_1_2, R.drawable.joconde_1_3,R.drawable.joconde_1_4,
        R.drawable.joconde_2_1, R.drawable.joconde_2_2, R.drawable.joconde_2_3,R.drawable.joconde_2_4,
        R.drawable.joconde_3_1, R.drawable.joconde_3_2, R.drawable.joconde_3_3,R.drawable.joconde_3_4,
        R.drawable.joconde_4_1, R.drawable.joconde_4_2, R.drawable.joconde_4_3,R.drawable.joconde_4_4,
};

public ImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    return mMona.length;
}

@Override
public Object getItem(int position) {
    return mMona[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mMona[position]);
    return imageView;
}

}

活动类

 ImageAdapter imageAdapter = new ImageAdapter(this);
        gridView.setAdapter(imageAdapter);

1 个答案:

答案 0 :(得分:1)

您可以将int数组作为Image Adapter的参数。该数组在图像数组中具有图像所需的位置,可以返回临时数组或图像。我没有测试过,但我认为我应该工作。 改变了代码就在这里..

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;    
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter{
    private Context mContext;
    Integer []retuning;

// Keep all Images in array
public Integer[] mMona = {
        R.drawable.joconde_1_1, R.drawable.joconde_1_2, R.drawable.joconde_1_3,R.drawable.joconde_1_4,
        R.drawable.joconde_2_1, R.drawable.joconde_2_2, R.drawable.joconde_2_3,R.drawable.joconde_2_4,
        R.drawable.joconde_3_1, R.drawable.joconde_3_2, R.drawable.joconde_3_3,R.drawable.joconde_3_4,
        R.drawable.joconde_4_1, R.drawable.joconde_4_2, R.drawable.joconde_4_3,R.drawable.joconde_4_4,
};



public ImageAdapter(Context c, int ...pos){
    mContext = c;
    //Added this line...
    retuning= new Integer[pos.length];
    for(int i=0;i<pos.length;i++){
        if(i<mMona.length-1){
            retuning[i]=mMona[pos[i]];
        }
    }
}

@Override
public int getCount() {
    return retuning.length;
}

@Override
public Object getItem(int position) {
    return retuning[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mMona[position]);
    return imageView;
}
}

在您的活动类中调用此适配器,如下所示..

int []img={0,2,3,5};
ImageAdapter imageAdapter = new ImageAdapter(this,img);
gridView.setAdapter(imageAdapter);