所以,我正在尝试创建一个屏幕,当点击一个按钮时,它从一些EditText获取数据并将它们添加到ListView项目,现在我知道网上有很多例子,我已经完成了他们和他们工作,但当我把他们带到我想做的事情时它只是添加一个项目并停止工作它不会抛出任何异常或任何东西,它只是添加一个项目,这是我到目前为止...
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;
}
}
最后是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);
}
});
}
}
答案 0 :(得分:0)
Activity
上下文来实例化您的适配器,而不是应用程序上下文。shifts
中存储构建列表的副本可以是dangerous。shifts
存放在持有人中。只有意见进入。getItem(position)
方法以获取Actividad
数据...而不是shifts
变量。答案 1 :(得分:0)
原来问题是ListView在ScrollView中,当列表动态增长时,这不能正常工作,我将它移出ScrollView,它就像魅力一样。
这是Android项目中的according to @Romain Guy开发人员,他说明了
使用ListView使其不滚动是非常昂贵的,并违背ListView的整个目的。你不应该这样做。只需使用LinearLayout。
所以我使用LinearLayout在布局上绘制新项目。