2012-02-28 57 views
0

我正在填充包含ImageView和TextView的ListView。它不会崩溃,但也不会显示任何内容。我看到很多教程,但似乎显示了与我使用的代码相同的代码。带有ImageView和TextView的ListView不显示任何内容

这是row.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/fondo_fila_click" 
    android:orientation="horizontal" 
    android:padding="5dip" > 

    <LinearLayout android:id="@+id/thumbnail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="3dip" 
     android:layout_alignParentLeft="true" 
     android:background="@drawable/borde_foto" 
     android:layout_marginRight="5dip"> 

     <ImageView 
      android:id="@+id/imagen_marca" 
      android:layout_width="50dip" 
      android:layout_height="50dip"/> 

    </LinearLayout> 

    <TextView 
     android:id="@+id/nombre" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/thumbnail" 
     android:layout_toRightOf="@+id/thumbnail" 
     android:textColor="#040404" 
     android:typeface="sans" 
     android:textSize="15dip" 
     android:textStyle="bold"/> 


</RelativeLayout> 

,代码:

public class SATActivity extends SherlockListActivity { 

private IconListViewAdapter adaptador; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ArrayList m_fabricante = new ArrayList(); 
     this.adaptador = new IconListViewAdapter(this, R.layout.fila, m_fabricante); 
     setListAdapter(this.adaptador);   
     iniciaFabricantes();   
    } 

    @SuppressWarnings("unchecked") 
    private void iniciaFabricantes() { 
     ArrayList m_fabricante = new ArrayList(); 

     try { 
      Fabricante acer = new Fabricante(); 
      acer.setNombre("Acer"); 
      acer.setFoto(R.drawable.acer); 
      Fabricante apple = new Fabricante(); 
      apple.setNombre("Apple"); 
      apple.setFoto(R.drawable.apple); 

      m_fabricante.add(acer); 
      m_fabricante.add(apple); 
     } catch (Exception e) { 

     } 

     adaptador.notifyDataSetChanged(); 
    } 

    public class IconListViewAdapter extends ArrayAdapter<Fabricante> { 

     private ArrayList<Fabricante> items; 

     public IconListViewAdapter(Context context, int textViewResourceId, ArrayList<Fabricante> items) { 
      super(context, textViewResourceId, items); 
      this.items = items; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.row, null); 
      } 

      Fabricante o = items.get(position); 
      if (o != null) { 

       //poblamos la lista de elementos 

       TextView tt = (TextView) v.findViewById(R.id.nombre); 
       ImageView im = (ImageView) v.findViewById(R.id.imagen_marca); 

       if (im!= null) { 
        im.setImageResource(o.getFoto()); 
       }  

       if (tt != null) {    
        tt.setText(o.getNombre());        
       }              
      } 
      return v; 
     } 
    } 
} 

有谁知道为什么它不显示文本或图像?

感谢

回答

1

试着改变你的适配器类以下之一:

public class IconListViewAdapter extends ArrayAdapter<Fabricante> { 

     private ArrayList<Fabricante> items; 
     LayoutInflater mInflater; 

     public IconListViewAdapter(Context context, int textViewResourceId, ArrayList<Fabricante> items) { 
      super(context, textViewResourceId, items); 
      this.items = items; 
      mInflater = LayoutInflater.from(context); 
     } 

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

      final ViewHolder holder; 

      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.row, null); 
       holder = new ViewHolder(); 

       holder.mTextView=(TextView)convertView.findViewById(R.id.nombre); 
       holder.mImageView=(ImageView)convertView.findViewById(R.id.imagen_marca); 
       convertView.setTag(holder); 
      } 
      else 
       holder=(ViewHolder)convertView.getTag(); 

      Fabricante o = items.get(position); 
      if (o != null) { 

       //poblamos la lista de elementos 

       holder.mImageView.setImageResource(o.getFoto()); 

       holder.mTextView.setText(o.getNombre());        

      } 
      return convertView; 
     } 
     static class ViewHolder 
     {     
      TextView mTextView; 
      ImageView mImageView; 
     }  

    } 
0

在你getView方法,进行以下更改:

if (im!= null) { 
    Drawable d = context.getResources().getDrawable(o.getFoto()); 
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());//must require 

    if(d!=null) 
    im.setImageDrawable(d); 
    else 
    Log.w("listview", "drawable is not available"); 
} 

希望那么它应该工作。

+0

这个代码不工作,我把im.setimagedrawable(d),但其结果相同 – colymore 2012-02-28 17:38:06

+0

检查我更新的答案。 – waqaslam 2012-02-28 17:46:52

+0

什么也没有,并且logcat不显示错误.. – colymore 2012-02-28 17:52:54