2011-10-21 156 views
2

我尝试以下方式,但我得到空指针异常在iv.setImageBitmap(bm)。 我尝试使用网格视图和布局Inflater.I也尝试设置图像与文本我也做了调试过程,但仍然无法获得解决方案。请检查下面的代码。如何在gridview中设置图像的下方有文字?

public class CategoryAdapter extends BaseAdapter { 
private Context mContext; 
public static final int ACTIVITY_CREATE = 10; 
int id=-1; 
public CategoryAdapter(Context c) { 
     mContext = c; 
} 
public int getCount() { 
    return CategorylogoActivity.logoarray.length; 
} 

public Object getItem(int position) { 
    return position; 
} 

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

public View getView(int position, View convertView, ViewGroup parent) { 
     Bitmap bm=null; 
     ImageView iv = null ; 
     TextView tv=null; 
     View v; 
        if(convertView==null){ 

         @SuppressWarnings("static-access") 
         LayoutInflater li = (LayoutInflater)mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE); 

         v = li.inflate(R.layout.category_icon, null); 
         tv= (TextView)v.findViewById(R.id.icon_text); 

         iv = (ImageView)v.findViewById(R.id.icon_image); 


        } 
        else 
        { 
         v = convertView; 
        } 
         try { 
          URL aURL = new URL(CategorylogoActivity.logoarray[position]); 
          URLConnection conn = aURL.openConnection(); 
          conn.connect(); 
          InputStream is = conn.getInputStream(); 
          // Buffered is always good for a performance plus. 
          BufferedInputStream bis = new BufferedInputStream(is); 
          // Decode url-data to a bitmap. 
          bm = BitmapFactory.decodeStream(bis); 

          bis.close(); 
          is.close(); 
         } catch (MalformedURLException e) { 

          e.printStackTrace(); 
         } catch (IOException e) { 

          e.printStackTrace(); 
         } 
        iv.setImageBitmap(bm); 
        tv.setText(CategorylogoActivity.namearray[0]); 

        return v; 

回答

0
static class viewHolder { 
     ImageView iv = null ; 
     TextView tv=null; 
} 


public View getView(int position, View convertView, ViewGroup parent) { 
     Bitmap bm=null; 
     viewHolder vh; 
     View v; 
        if(convertView==null){ 
         vh = new viewHolder(); 
         @SuppressWarnings("static-access") 
         LayoutInflater li = (LayoutInflater)mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE); 


         v = li.inflate(R.layout.category_icon, null); 
         vh.tv= (TextView)v.findViewById(R.id.icon_text); 

         vh.iv = (ImageView)v.findViewById(R.id.icon_image); 

         v.setTag(vh); 
        } 
        else 
        { 
         vh = (viewHolder)convertView.getTag(); 
         v = convertView; 
        } 
         try { 
          URL aURL = new URL(CategorylogoActivity.logoarray[position]); 
          URLConnection conn = aURL.openConnection(); 
          conn.connect(); 
          InputStream is = conn.getInputStream(); 
          // Buffered is always good for a performance plus. 
          BufferedInputStream bis = new BufferedInputStream(is); 
          // Decode url-data to a bitmap. 
          bm = BitmapFactory.decodeStream(bis); 

          bis.close(); 
          is.close(); 
         } catch (MalformedURLException e) { 

          e.printStackTrace(); 
         } catch (IOException e) { 

          e.printStackTrace(); 
         } 
        vh.iv.setImageBitmap(bm); 
        vh.tv.setText(CategorylogoActivity.namearray[0]); 

        return v; 

你必须使用viewHolder样的对象,当convertView不为空,你的IV ImageView的为空。所以你对null对象使用setImageBitmap()。试试我的代码。

HTH。

+0

感谢您对我的查询做出快速而准确的回答。感谢您的分配。 –

0

你不应该在你的适配器做的URLConnection,这会尽量做到倍数连接,导致getView()会在您每次显示适配器这一观点时调用。在一个单独的类中处理连接。

相关问题