2

在这里,我正在尝试创建一个包含班级校友姓名和 ID 的列表,但是,我很难在列表中显示它们,它们看起来像这样

我试图将数据放入活动列表中的方式是:

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);

这是所涉及的每个类的来源:

校友:

/** 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(列表行中的文本视图和复选框所在的位置):

<?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>

我想这一切都与此相关。我知道这可能是一个愚蠢的错误,但是男孩,我似乎无法找到问题所在。

4

1 回答 1

0

我认为您的所有问题都出在 simplerow.xml 文件中。您使用的是 RelativeLayout 但您没有定位您的文本视图。
所以文本视图最终会相互重叠。
例如,如果像这样更改您的 simplerow.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" >

  <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>
于 2012-11-22T01:35:03.227 回答