我正在填充包含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;
}
}
}
有誰知道爲什麼它不顯示文本或圖像?
感謝
這個代碼不工作,我把im.setimagedrawable(d),但其結果相同 – colymore 2012-02-28 17:38:06
檢查我更新的答案。 – waqaslam 2012-02-28 17:46:52
什麼也沒有,並且logcat不顯示錯誤.. – colymore 2012-02-28 17:52:54