MainActivity:
public class MainActivity extends Activity {
ListView lista;
Context c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c=getApplicationContext();
ArrayList<Seleccion> seleccion=new ArrayList<Seleccion>();
seleccion.add(new Seleccion("David", "Fernández", R.drawable.ic_launcher));
seleccion.add(new Seleccion("Fernando", "Pérez", R.drawable.ic_launcher));
seleccion.add(new Seleccion("Antonio", "López", R.drawable.ic_launcher));
seleccion.add(new Seleccion("María", "Domínguez", R.drawable.ic_launcher));
ListaAdapter listAdapter=new ListaAdapter(c, seleccion);
lista=(ListView)findViewById(R.id.listView1);
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});
lista.setAdapter(listAdapter);
}
}
自定义适配器:
公共类ListaAdapter扩展了BaseAdapter {
private static int contador=0;
ArrayList<Seleccion>seleccion;
private LayoutInflater inflater = null;
static class ViewHolder{
TextView nombre;
TextView apellido;
int imagen;
}
public ListaAdapter(Context c, ArrayList<Seleccion> seleccion) {
Log.i("LISTA", "creating adapter");
this.seleccion=seleccion;
inflater = LayoutInflater.from(c);
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
Log.i("LIST", "Returned "+seleccion.size()+" results");
return seleccion.size();
}
@Override
public Object getItem(int position) {
Log.i("LIST", "one item returned, name: "+seleccion.get(position).getNombre());
return seleccion.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView=inflater.inflate(R.layout.listitemlayout, null);
contador++;
Log.i("LIST", contador+" vews created");
holder=new ViewHolder();
holder.nombre=(TextView)convertView.findViewById(R.id.textView1);
holder.apellido=(TextView) convertView.findViewById(R.id.textView2);
holder.imagen=R.id.imageView1;
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
Seleccion sel=(Seleccion)getItem(position);
Log.i("LIST", "Name "+sel.getNombre());
holder.nombre.setText(sel.getNombre());
holder.apellido.setText(sel.getApellidos());
holder.imagen=sel.getImagen();
}
return convertView;
}
}
自定义适配器的布局:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_alignTop="@drawable/ic_launcher"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView"
android:textSize="20dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
MainActivity布局......
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
我在表中有四行,但只有第一行接收值。其他的仍保留适配器布局中的默认文本集。我不知道错误在哪里...任何帮助?谢谢。
答案 0 :(得分:0)
将您的getView更改为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView=inflater.inflate(R.layout.listitemlayout, null);
contador++;
Log.i("LIST", contador+" vews created");
holder=new ViewHolder();
holder.nombre=(TextView)convertView.findViewById(R.id.textView1);
holder.apellido=(TextView) convertView.findViewById(R.id.textView2);
holder.imagen=R.id.imageView1;
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
Seleccion sel=(Seleccion)getItem(position);
Log.i("LIST", "Name "+sel.getNombre());
holder.nombre.setText(sel.getNombre());
holder.apellido.setText(sel.getApellidos());
holder.imagen=sel.getImagen();
return convertView;
}
答案 1 :(得分:0)
症状相同但对我不同。我将片段布局更改为受控高度而不是match_parent,这解决了我的问题。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
到
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >