在这里,我正在尝试使用类'列的名称和id创建一个列表,但是,我很难尝试在列表中显示它们,它们看起来像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);
以下是每个课程的来源:
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;
}
}
现在布局文件:
activity_alumnos_asistencia.xml(列表应该是):
<?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(列表行中的textviews和复选框):
<?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>
我想这与此有关。我知道这可能是一个愚蠢的错误,但男孩,我似乎无法找到问题所在。
答案 0 :(得分:0)
我认为你的所有问题都存在于simplerow.xml文件中。您使用RelativeLayout但未定位文本视图 因此,文本视图最终相互重叠 例如,如果像这样更改simplerow.xml,那么一个textview将显示在另一个下面:
<?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" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<TextView
android:id="@+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="text1"
android:textSize="16sp" />
<TextView
android:id="@+id/rowTextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="text2"
android:textSize="16sp" />
</LinearLayout>
<CheckBox
android:id="@+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:padding="10dp" />
</RelativeLayout>