0

So, I'm trying to create a screen that when clicking a button this takes the data from some EditText and add them to a ListView item, now I know there are a lot of examples on the web and I've done them and they work, but when I took them to what I want to do it just adds one item and stops working it doesn't throw any exception or anything, it just add one item, this is what I got so far...

create_class.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.eduardolaguna.mariela.app.activities.CreateClass">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/cc_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/cc_hint_class_name" />

            <View
                android:id="@+id/cc_divider1"
                style="@style/Divider"
                android:layout_below="@+id/cc_name" />

            <TextView
                android:id="@+id/tv_cc_professors_data"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/cc_divider1"
                android:text="@string/cc_tv_professors_data" />

            <EditText
                android:id="@+id/cc_professors_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_cc_professors_data"
                android:hint="@string/cc__hint_professors_name"
                android:inputType="textPersonName|textAutoComplete|textAutoCorrect" />

            <EditText
                android:id="@+id/cc_professors_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/cc_professors_name"
                android:hint="@string/cc_hint_professors_email"
                android:inputType="textEmailAddress" />

            <EditText
                android:id="@+id/cc_professors_phonenumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/cc_professors_email"
                android:hint="@string/cc_hint_professors_phonenumber"
                android:inputType="phone" />

            <View
                android:id="@+id/cc_divider2"
                style="@style/Divider"
                android:layout_below="@id/cc_professors_phonenumber" />

            <TextView
                android:id="@+id/tv_cc_schedule"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/cc_divider2"
                android:text="@string/cc_tv_class_schedule" />

            <Spinner
                android:id="@+id/sp_cc_day_of_the_week"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_cc_schedule"
                android:entries="@array/dow" />

            <EditText
                android:id="@+id/cc_from_schedule"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@id/sp_cc_day_of_the_week"
                android:hint="@string/cc_hint_from_schedule"
                android:inputType="time" />

            <EditText
                android:id="@+id/cc_to_schedule"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/sp_cc_day_of_the_week"
                android:layout_toRightOf="@id/cc_from_schedule"
                android:hint="@string/cc_hint_to_schedule"
                android:inputType="time" />

            <EditText
                android:id="@+id/cc_floor_number"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/sp_cc_day_of_the_week"
                android:layout_toRightOf="@id/cc_to_schedule"
                android:hint="@string/cc_hint_floor"
                android:inputType="number" />

            <EditText
                android:id="@+id/cc_classroom"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/sp_cc_day_of_the_week"
                android:layout_toRightOf="@id/cc_floor_number"
                android:hint="@string/cc_hint_classroom" />

            <Button
                android:id="@+id/btn_cc_add_schedule"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/cc_from_schedule"
                android:text="@string/cc_add_schedule" />

            <ListView
                android:id="@+id/cc_schedule_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/btn_cc_add_schedule" />
        </RelativeLayout>
    </ScrollView>
</LinearLayout>

schedule_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/sch_day_of_the_week"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Miércoles"
        android:textSize="60dp" />

    <TextView
        android:id="@+id/sch_from"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="5:45"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/sch_to"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="7:15"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/sch_floor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="7"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/sch_clasroom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="lab1"
        android:textSize="20dp" />
</LinearLayout>

ScheduleAdapter.java

public class ScheduleAdapter extends ArrayAdapter<Actividad> {

    private int layoutResourceId;

    private LayoutInflater inflater;

    private List<Actividad> shifts;


    public ScheduleAdapter(Context context, int resource, List<Actividad> objects) {
        super(context, resource, objects);
        layoutResourceId = resource;
        shifts = objects;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        Holder holder = null;

        row = inflater.inflate(layoutResourceId, null);

        holder = new Holder();
        holder.actividad = shifts.get(position);
        holder.dow = (TextView) row.findViewById(R.id.sch_day_of_the_week);
        holder.fromTXT = (TextView) row.findViewById(R.id.sch_from);
        holder.toTXT = (TextView) row.findViewById(R.id.sch_to);
        holder.floorTXT = (TextView) row.findViewById(R.id.sch_floor);
        holder.roomTXT = (TextView) row.findViewById(R.id.sch_clasroom);

        setupItem(holder);
        row.setTag(holder);
        return row;
    }

    private void setupItem(Holder holder) {
        holder.dow.setText(holder.actividad.getDiaDeLaSemana());
        holder.fromTXT.setText(holder.actividad.getDesdeStr());
        holder.toTXT.setText(holder.actividad.getHastaStr());
        holder.floorTXT.setText(holder.actividad.getPiso());
        holder.roomTXT.setText(holder.actividad.getSalon());
    }

    public static class Holder {
        Actividad actividad;
        TextView dow;
        TextView fromTXT;
        TextView toTXT;
        TextView floorTXT;
        TextView roomTXT;
    }
}

And finally the CreateClass.java public class CreateClass extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_class);

        ListView view = (ListView) findViewById(R.id.cc_schedule_list);
        final ScheduleAdapter adapter = new ScheduleAdapter(getApplicationContext(), R.layout.schedule_item, new ArrayList<Actividad>());
        view.setAdapter(adapter);

        Button addSchedule = (Button) findViewById(R.id.btn_cc_add_schedule);
        addSchedule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Resources res = getResources();

                ListView scheduleView = (ListView) findViewById(R.id.cc_schedule_list);
                ScheduleAdapter adapter1 = (ScheduleAdapter) scheduleView.getAdapter();

                Actividad act = new Actividad(TipoEvento.CLASE);
                Spinner dow = (Spinner) findViewById(R.id.sp_cc_day_of_the_week);
                act.setDiaDeLaSemana(dow.getSelectedItem().toString());

                TextView from = (TextView) findViewById(R.id.cc_from_schedule);
                act.setDesdeStr(res.getString(R.string.from) + ": " + from.getText().toString());

                TextView to = (TextView) findViewById(R.id.cc_to_schedule);
                act.setHastaStr(res.getString(R.string.to) + ": " + to.getText().toString());

                TextView floor = (TextView) findViewById(R.id.cc_floor_number);
                act.setPiso(res.getString(R.string.floor) + ": " + floor.getText().toString());

                TextView classroom = (TextView) findViewById(R.id.cc_classroom);
                act.setSalon(res.getString(R.string.classroom) + ": " + classroom.getText().toString());

                adapter1.add(act);
            }
        });
    }
}
Eduardo
  • 133
  • 3
  • 8

2 Answers2

0
  1. You should instantiate your adapter with the Activity context, not the application context.
  2. Storing a copy of the constructed list in shifts can be dangerous.
  3. Don't store shifts in the Holder. Only views go in there.
  4. When populating the convertView, reference the internal getItem(position) method to obtain the Actividad data...not your shifts variable.
  5. You're also using the ViewHolder paradigm incorrectly. Example here.
Community
  • 1
  • 1
Ifrit
  • 6,791
  • 8
  • 50
  • 79
0

Turns out the problem is the ListView is inside the ScrollView, this does not work properly when the list grows dynamically, I move it out the ScrollView and it works like a charm.

This was according to @Romain Guy a developer in the Android project, where he stated that

Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.

So I use a LinearLayout to draw the new items on the layout.

Community
  • 1
  • 1
Eduardo
  • 133
  • 3
  • 8