Here, I’m trying to create a list with the name and ids of a class’ alumns, however, I’m having a hard time trying to display them in the list, they appear like this:
The way I’m trying to put data in the list in the activity is this:
ListView mainListView = (ListView) findViewById(R.id.listaAlumnos);
ArrayList<Alumno> AlumnoList = new ArrayList<Alumno>();
AlumnoList.addAll(Arrays.asList(Alumnos));
AlumnoArrayAdapter<Alumno> listAdapter = new AlumnoArrayAdapter(this, AlumnoList);
mainListView.setAdapter(listAdapter);
Here is the source of every class involved:
Alumno:
/** Holds Alumno data. */
class Alumno {
private String name = "";
private String noControl = "";
private boolean checked = false;
public Alumno() {
}
public Alumno(String name, String noControl) {
this.name = name;
this.noControl = noControl;
}
public Alumno(String name, boolean checked) {
this.name = name;
this.checked = checked;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNoControl() {
return noControl;
}
public void setNoControl(String noControl) {
this.noControl = noControl;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String toString() {
return name;
}
public void toggleChecked() {
checked = !checked;
}
}
AlumnoArrayAdapter:
/** Custom adapter for displaying an array of Alumno objects. */
class AlumnoArrayAdapter extends ArrayAdapter<Alumno> {
private LayoutInflater inflater;
public AlumnoArrayAdapter(Context context, List<Alumno> AlumnoList) {
super(context, R.layout.simplerow, R.id.rowTextView, AlumnoList);
// Cache the LayoutInflate to avoid asking for a new one each time.
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Alumno to display
Alumno Alumno = (Alumno) this.getItem(position);
// Componentes de cada fila
CheckBox checkBox;
TextView textView1;
TextView textView2;
// Create a new row view
if (convertView == null) {
convertView = inflater.inflate(R.layout.simplerow, null);
// Find the child views.
textView1 = (TextView) convertView.findViewById(R.id.rowTextView);
textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
// Optimization: Tag the row with it's child views, so we don't
// have to
// call findViewById() later when we reuse the row.
convertView.setTag(new AlumnoViewHolder(textView1, textView2, checkBox));
// If CheckBox is toggled, update the Alumno it is tagged with.
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Alumno Alumno = (Alumno) cb.getTag();
Alumno.setChecked(cb.isChecked());
}
});
}
else {
// Because we use a ViewHolder, we avoid having to call
// findViewById().
AlumnoViewHolder viewHolder = (AlumnoViewHolder) convertView.getTag();
checkBox = viewHolder.getCheckBox();
textView1 = viewHolder.getTextView1();
textView2 = viewHolder.getTextView2();
}
// Tag the CheckBox with the Alumno it is displaying, so that we can
// access the Alumno in onClick() when the CheckBox is toggled.
checkBox.setTag(Alumno);
checkBox.setChecked(Alumno.isChecked());
textView1.setText(Alumno.getName());
textView2.setText(Alumno.getNoControl());
return convertView;
}
}
AlumnoViewHolder:
/** Holds child views for one row. */
class AlumnoViewHolder {
private CheckBox checkBox;
private TextView textView1;
private TextView textView2;
public AlumnoViewHolder() {
}
public AlumnoViewHolder(TextView textView1, TextView textView2, CheckBox checkBox) {
this.checkBox = checkBox;
this.textView1 = textView1;
this.textView2 = textView2;
}
public CheckBox getCheckBox() {
return checkBox;
}
public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}
public TextView getTextView1() {
return textView1;
}
public void setTextView1(TextView textView1) {
this.textView1 = textView1;
}
public TextView getTextView2() {
return textView2;
}
public void setTextView2(TextView textView2) {
this.textView2 = textView2;
}
}
And now the layout files:
activity_alumnos_asistencia.xml (where the list is supposed to be):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/findSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Registrar Asistencia" />
<ListView
android:id="@+id/listaAlumnos"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
simplerow.xml (where the textviews and checkboxes from the list’s rows are):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
<TextView android:id="@+id/rowTextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
<CheckBox android:id="@+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentRight="true" android:layout_marginRight="6sp"
android:focusable="false">
</CheckBox>
</RelativeLayout>
I guess that is all the relevant for this. I know it might be a stupid mistake, but boy, I cant seems to find where the issue is.
I think all your problem lays in the simplerow.xml file. You using a RelativeLayout but you are not positioning your textviews.
So the textviews ends up overlapping on each other.
For example if change your simplerow.xml like this, you will that one textview will appear under the other: