我正在填充包含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;
}
}
}
有谁知道为什么它不显示文字或图像?
由于
答案 0 :(得分:1)
尝试将适配器类更改为低于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;
}
}
答案 1 :(得分: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");
}
希望它应该有用。
答案 2 :(得分:0)
尝试添加ViewHolder以及在if语句中调用findViewById,在其中展开视图。 http://www.vogella.de/articles/AndroidListView/article.html#overview应该有助于您的自定义适配器。