将整数转换为Drawable [英] Converting an Integer to a Drawable

查看:250
本文介绍了将整数转换为Drawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Adapter类,在第一个中,我有一个名为mThumbIds的整数数组[],它初始化如下:

I have two Adapter classes and in the first one, i have an Array of Integers[] named mThumbIds which is initialized like this :

    // References to our images
private Integer[] mThumbIds = {
        R.mipmap.beyondthe_ferocious_flash_majin_vegeta_icon,
        R.mipmap.full_tilt_kamehameha_super_saiyan2_gohan_youth_icon,
        R.mipmap.merciless_condemnation_goku_black_super_saiyan_rose_and_zamasu_icon,
        R.mipmap.everlasting_legend_super_saiyan_goku_icon,
        R.mipmap.indestructible_saiyan_evil_legendary_super_saiyan_broly_icon
};

我想从这个数组中提取一个Integer(让我们说mThumbIds [0])然后传递它进入我的其他适配器类并使用它来获取一个有效的drawable,我可以用于我的imageViews。

I want to extract an Integer from this array(let's say mThumbIds[0]) and then pass it into my other adapter class and use it to get a valid drawable that i can use for my imageViews.

这是第二个适配器的List来保存提取的Integer:

This is the 2nd adapter's List to hold the extracted Integer:

    // References to the images via a List
private List<Integer> mGLBIcons = new ArrayList<>();

这里是我需要抽奖的地方:

And here is where i need the drawable :

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    // If it's not recycled, initialize some attributes
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(225, 225));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }
     imageView.setImageResource(mGLBIcons.get(position));
    return imageView;
}

到目前为止,我尝试过的任何东西都没有用,所以我要求你的帮助!

So far, nothing that i've tried has worked so i am asking for your help!

EDITED

ImageViewAdapterClass:

ImageViewAdapterClass:

public class UserBoxGlbImageAdapter extends BaseAdapter {

Context mContext;

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

@Override
public int getCount() {
    return mGLBIcons.size();
}

@Override
public Object getItem(int i) {
    return null;
}

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

// References to the images via a List
private List<Integer> mGLBIcons = new ArrayList<>();

// Used to add card icons from the mainScreenFragment
public List<Integer> getGLBIconsList() {
    return mGLBIcons;
}

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    // If it's not recycled, initialize some attributes
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(225, 225));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }
     Drawable drbl = mContext.getResources().getDrawable(mGLBIcons.get(0));
     imageView.setImageDrawable(drbl);
    return imageView;
}

public void passInteger(Integer integer) {
    mGLBIcons.add(integer);
}

}

GridSettingFragmentClass:

GridSettingFragmentClass:

public class UserBoxGLBFragment extends Fragment {

GridView globalGridView;

public UserBoxGLBFragment() {
    // Required empty public constructor
}

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_user_box_glb, container, false);

    globalGridView = view.findViewById(R.id.userBoxGlbGridView);
    globalGridView.setAdapter(new UserBoxGlbImageAdapter(getContext()));
    return view;
}
}

fragment_user_box_glb.xml:

fragment_user_box_glb.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorSecondaryGLB"
android:orientation="vertical"
tools:context="com.dcv.spdesigns.dokkancards.ui.UserBoxGLBFragment">

<GridView
    android:id="@+id/userBoxGlbGridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="4"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" >

</GridView>

</LinearLayout>


推荐答案

您正在使用mipmap id并且您正在尝试加载它是一个可绘制的,我怀疑它会起作用。尝试将图像放在drawable文件夹中并使用thr drawable id。

you are using mipmap id and you are trying to load it as a drawable, i doubt it will work. try putting the images in the drawable folder and use thr drawable id.

这是在适配器中设置可绘制图像的方法:

this is how you set a drawable image in a adapter:

imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_success))

在你的情况下它将是

imageView.setImageDrawable(context.getResources().getDrawable(listOfImages[position]))

更改构造函数:

public UserBoxGlbImageAdapter(Context c, List<Integer> iconsList) {
    mContext = c;
mGLBIcons = iconsList;
}

尝试使用如下:

image_view_layout xml:

image_view_layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="225dp"
        android:layout_height="225dp" />

</LinearLayout>

public class UserBoxGLBFragment extends Fragment {

GridView globalGridView;

List<Integer> listOfimages = new ArrayList<>();
public UserBoxGLBFragment() {
    // Required empty public constructor
}

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_user_box_glb, container, false); 
    listOfimages.add(R.drawable.beyondthe_ferocious_flash_majin_vegeta_icon);
    globalGridView = view.findViewById(R.id.userBoxGlbGridView);
    globalGridView.setAdapter(new UserBoxGlbImageAdapter(getContext(),listOfimages));
    return view;
}
}


public class UserBoxGlbImageAdapter extends BaseAdapter {

Context mContext;

// References to the images via a List
private List<Integer> mGLBIcons = new ArrayList<>();


public UserBoxGlbImageAdapter(Context c,List<Integer> listOfIcons) {
    mContext = c;
    mGLBIcons = listOfIcons;
}

@Override
public int getCount() {
    return mGLBIcons.size();
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}
// Used to add card icons from the mainScreenFragment
public List<Integer> getGLBIconsList() {
    return mGLBIcons;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

        // If it's not recycled, initialize some attributes
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_view_layout, parent, false));

        } 
        ImageView imageView = (ImageView)convertView.findViewById(R.id.image_view)
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);     imageView.setImageDrawable(mContext.getResources().getDrawable(mGLBIcons.get(0)));
            return imageView;
   }


}

这篇关于将整数转换为Drawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆