2

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.

Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
siiimmm
  • 23
  • 2

1 Answers1

0

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:

  <?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>
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • perhaps if he'd like to center the checkbox too: `android:layout_centerVertical="true"` – mango Nov 22 '12 at 01:39
  • Yeah maybe. We can not guess what he is trying to do, we can just tell him the flaws. – Lazy Ninja Nov 22 '12 at 01:41
  • @LazyNinja First of all, thanks for answering. Now, made those changes, sadly, got the same result. What I'm trying to do is something like this http://i.imgur.com/jXTgN.png, but having a hard time, lol. I know it must be an easy thing to do, but I'm just starting with Android, and so far liking it, hehe... but wow, cant find the issue in that code. – siiimmm Nov 22 '12 at 04:47
  • Yeah, I understand! Now that I know what you want to do, it is easier. See my edit, that should work for you. – Lazy Ninja Nov 22 '12 at 05:13
  • @LazyNinja Hello, thanks again. Sadly, now the app crash when trying to access that list, this is really driving me crazy!, thanks anyways, appreciate it! – siiimmm Nov 22 '12 at 05:35
  • @siiimmm, dont give up. We are here to help. Can you post the logcat trace when the application crashes. – Lazy Ninja Nov 22 '12 at 05:37
  • Either you have duplicated id or eclipse has messed up things. Clean project and retry. See here http://stackoverflow.com/a/11191304/1503155 – Lazy Ninja Nov 22 '12 at 06:00
  • @LazyNinja Oh wow, cleaning the project actually worked, now the list is working perfectly!, thanks man, really appreciate it!. Any idea on what could have caused it? – siiimmm Nov 22 '12 at 06:03
  • It is an eclipse issue. Sometimes it messes up your project. So if you get some weird errors it is wise to clean your project. Please upvote my answer. I will upvote your question so you can mark this as an answer. – Lazy Ninja Nov 22 '12 at 06:26