2012-02-28 99 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

相關問題