2015-03-02 70 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

相關問題