2017-04-10 184 views
-2

我想從字符串獲取ImageView到picture.setImageResource();我使用字符串來整數變化,它不顯示在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

這將有助於你得到相應的圖像的ID在setImageResource()使用

int 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); 
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' 
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, 
    }; 

} 
+0

你好,@Simranjeet辛格圖像被來自服務器內部列表

的getImage()是字符串不是int,如何從中獲得? –