简体   繁体   English

Android ListView和ArrayAdapter实现

[英]Android ListView and ArrayAdapter implementation

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... 因此,我正在尝试创建一个屏幕,当单击按钮时,该屏幕将从一些EditText中获取数据并将其添加到ListView项中,现在我知道网络上有很多示例,我已经完成了,工作,但是当我把他们带到我想做的事情时,它只会添加一项并停止工作,不会抛出任何异常或任何东西,它只会添加一项,这就是我到目前为止所得到的...

create_class.xml 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 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 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 { 最后,CreateClass.java公共类CreateClass扩展了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);
            }
        });
    }
}
  1. You should instantiate your adapter with the Activity context, not the application context. 您应该使用Activity上下文而不是应用程序上下文实例化适配器。
  2. Storing a copy of the constructed list in shifts can be dangerous . shifts存储构造好的列表的副本可能很危险
  3. Don't store shifts in the Holder. 不要将shifts存储在持有人中。 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. 填充convertView时,请引用内部getItem(position)方法以获得Actividad数据,而不是shifts变量。
  5. You're also using the ViewHolder paradigm incorrectly. 您还错误地使用了ViewHolder范例。 Example here . 这里的例子

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. 原来的问题是ListView在ScrollView内部,当列表动态增长时,它不能正常工作,我将其移出ScrollView,它就像一个吊饰一样工作。

This was according to @Romain Guy a developer in the Android project, where he stated that 这是根据 Android项目开发人员@Romain Guy说法 ,他表示

Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. 使用ListView使其不滚动非常昂贵,并且违背了ListView的整个目的。 You should NOT do this. 您不应该这样做。 Just use a LinearLayout instead. 只需使用LinearLayout即可。

So I use a LinearLayout to draw the new items on the layout. 因此,我使用LinearLayout在布局上绘制新项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM