-1

我正在开发墙纸应用程序我实现了此代码以便从资产 加载图像从资产文件夹加载我的图片

它工作得很好,但我从列表中获取 itemid 以使用 id 在全屏活动或设置壁纸中显示这些图像之一时遇到问题

public long getItemId(int position) {

    return position ;
}

这是我的代码

public class ImageAdapter extends BaseAdapter{
 private Context mContext;
private List<String> list;
private AssetManager assetManager;

public ImageAdapter(Context c, List<String> list) {
    mContext = c;
   this.list = list;

}

public int getCount() {

    return list.size();
}

public Object getItem(int position) {

    return position;
}

public long getItemId(int position) {

    return position;
}

public View getView(int position, View view, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);

    try {

        InputStream is = mContext.getAssets().open(list.get(position));
        Bitmap bm = BitmapFactory.decodeStream(is);
        imageView.setImageBitmap(bm);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
    } catch (IOException e) {

        e.printStackTrace();
    }
    // TODO Auto-generated method stub
    return imageView;
}



public class Gs3Wallpaper extends Activity {
private  ImageButton Gs3Button;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView gridView = (GridView) findViewById(R.id.GridView1);
    try {
        gridView.setAdapter(new ImageAdapter(this ,getImage()));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    public List<String> getImage()throws IOException {
    AssetManager assetManager = getAssets();
    String[] files = assetManager.list("gallary");
    List<String> list = new ArrayList<String>();
    for (String it : files) {
        list.add("gallary"+File.separator + it);        

    }
    return list;

我不知道如何从我尝试使用 list.get(position) 的列表中获取 id 但它返回字符串而不是 int 所以我不能将它传递给 setImageResource 例如

有什么建议么?

4

1 回答 1

0

HashMap您可以做的是在getView()设置列表项时设置图像或图像的路径,具体取决于您的操作方式。使用positionfor thekey和 image as thevalue

public class MyClass
{
    HashMap<String, Bitmap> imageMap = new HashMap<String, Bitmap>();
...

然后将其设置在您的getView()

public View getView(int position, View view, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);

try {

    InputStream is = mContext.getAssets().open(list.get(position));
    Bitmap bm = BitmapFactory.decodeStream(is);
    imageView.setImageBitmap(bm);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(80, 80));

    String posString = String.valueOf(position);
    imageMap.put(posString, bm);
} catch (IOException e) {
....
}

像这样的东西应该适合你。然后只需使用位置将位图从HashMap

于 2013-02-26T15:58:49.813 回答