2015-03-02 76 views
0

我有一个类,在数据库中得到两个字段(名称和图像),并设置名称文本浏览和图片设置为图像视图 现在我有形象问题,应该怎么用图像名称设置图像图像视图存储在数据库中? 请帮帮我!如何设置图像的图像视图的名称,存储在数据库中的Android?

我的源代码:


public class ListMovie extends BaseAdapter { 

    private Resources res = null; 
    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private LayoutInflater inflate; 
    ViewHolder holder; 

    public ListMovie(Activity a, ArrayList<HashMap<String, String>> d) { 
     // TODO Auto-generated constructor stub 

     activity = a; 
     data = d; 
     inflate = (LayoutInflater) activity 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return data.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public View getView(int arg0, View view, ViewGroup arg2) { 
     // TODO Auto-generated method stub 

     View v1 = view; 

     if (v1 == null) { 

      v1 = inflate.inflate(R.layout.item, null); 
      holder = new ViewHolder(); 

      holder.txt_name = (TextView) v1.findViewById(R.id.item_name); 

      holder.img = (ImageView) v1.findViewById(R.id.item_img); 


      v1.setTag(holder); 

     } else { 

      holder = (ViewHolder) v1.getTag(); 

     } 
     HashMap<String, String> song = new HashMap<String, String>(); 
     song = data.get(arg0); 


     holder.txt_name.setText(song.get("name")); 

     return v1; 

    } 
    static class ViewHolder { 
     TextView txt_name; 
     ImageView img; 
    } 
} 
+0

通过“映像名称'你的意思是图像文件存储位置的路径? – JonasCz 2015-03-02 20:06:57

+0

不只是存储图像的名字,并把图像绘制 – 2015-03-02 20:16:12

回答

0

如果您在/ res文件夹图像,你可以在getView使用:

int resourceId = res.getIdentifier(name, "drawable", 
    context.getPackageName());//initialize res and context in adapter's contructor 
img.setImageResource(resourceId); 

全码:

class ListMovie extends BaseAdapter { 

    private Resources res = null; 
    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private LayoutInflater inflate; 
    ViewHolder holder; 

    public ListMovie(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data = d; 
     inflate = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     res = a.getResources(); 
    } 
    @Override 
    public int getCount() { 
     return data.size(); 
    } 

    @Override 
    public HashMap<String, String> getItem(int position) { 
     return data.get(position); 
    } 

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

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

     if (view == null) { 
      view = inflate.inflate(R.layout.item, null); 
      holder = new ViewHolder(); 
      holder.txt_name = (TextView) view.findViewById(R.id.item_name); 
      holder.img = (ImageView) view.findViewById(R.id.item_img); 
      view.setTag(holder); 

     } else { 
      holder = (ViewHolder) view.getTag(); 
     } 
     HashMap<String, String> song = new HashMap<String, String>(); 
     song = data.get(position); 
     holder.txt_name.setText(song.get("name")); 
     int resourceId = res.getIdentifier(image_name, "drawable", 
       activity.getPackageName());//initialize res and context in adapter's contructor 
     img.setImageResource(resourceId); 
     return view; 

    } 
    static class ViewHolder { 
     TextView txt_name; 
     ImageView img; 
    } 
} 

变化image_name通过你的形象的名字

+0

错误()我应该怎么办? – 2015-03-03 08:53:55

+0

什么错误?在这里粘贴异常 – 2015-03-03 10:17:18

+0

上下文无法解析 – 2015-03-03 10:27:19

0

你可以使用Android Universal Image Loader库或ion动态加载图像。

编辑:

试试这个link。它有一个很好的例子来说明如何通过代码加载图像。

+0

没有,只是我想表明,存储在数据库中可绘制的图像通过名称图像与context.getPackageName发生 – 2015-03-02 20:20:58

相关问题