我想在picture.setImageResource( );
将StringView从String转换为Int我使用String to Integer Change,它不会在GridView中显示。如何获取图像大小和setImageView?
Menu.java
public class Menu {
private Integer id;
private String menuImage;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMenuImage() {
return menuImage;
}
public void setMenuImage(String menuImage) {
this.menuImage = menuImage;
}
MyAdapter.java
public class MyAdapter extends BaseAdapter {
List<Menu> mItems = new ArrayList<Item>(); // from server
LayoutInflater mInflater;
public MenuGridViewAdapter(Context context, List<Menu> menuList) {
mInflater = LayoutInflater.from(context);
this.menuList = menuList;
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Menu getItem(int i) {
return mItems.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if (v == null) {
v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
}
picture = (ImageView) v.getTag(R.id.picture);
Item item = getItem(i);
picture.setImageResource( ); //
return v;
}
答案 0 :(得分:0)
这将帮助您获取要在setImageResource()
中使用的相应图像的IDint id = getResources().getIdentifier("drawable/your_image", "id", "your_package_name");
imageView.setImageResource(id);
并且对于高度和宽度,您可以设置像这样的参数
LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.imgLayout2);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(340, 340);
parms.gravity = Gravity.CENTER;
parms.setMargins(20, 50, 20, 50);
final ImageView imageView = new ImageView(getActivity());
imageView.setLayoutParams(parms);
答案 1 :(得分:0)
imageView.setImageResource()需要来自Drawable的图像,这是一个int,例如:R.drawable.sample 您可以使用picasso库而不是setImageResource。为此,
添加以下行以将图像加载到imageview中。
Picasso.with(context).load(R.drawable.drawableName).into(picture);
在build.gradle文件中添加:
compile 'com.squareup.picasso:picasso:2.5.2'
答案 2 :(得分:0)
使用此代码:
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
public GridViewAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageUri(Uri.parse("your url here")); // updated code
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
};
}