私は学校プランナーアプリを実装しようとしています。タブレイアウトにラップされた毎日のリストビューとして実装されているタイムテーブルの概要があります。したがって、ユーザは月曜日から金曜日の間に切り替えることができ、特定の日の彼の時間割を得ることができる。ArrayAdapterとListViewには特定の項目のみが表示されます
public class TimetableAdapter extends ArrayAdapter<Lesson> {
private List<Lesson> lessonList; // The model
...
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
...
view = inflater.inflate(R.layout.timetable_row, null);
Lesson currentLesson = lessonList.get(position);
// Checks if selected Tab (context.getDay()) correspondends to
// currentLesson's day. If so the lesson will be rendered into
// the appropriated ListView. So if the user selects the Monday Tab
// he only wants to see the lessons for Monday.
if (context.getDay() == currentLesson.getWeekDay().getWeekDay()) {
fillView(currentLesson, holder); // render Lesson
}
...
return view;
}
private void fillView(Lesson currentLesson, ViewHolder holder) {
holder.subject.setText(currentLesson.getSubject().getName());
}
public class TimetableActivity extends Activity implements OnTabChangeListener {
public void onCreate(Bundle savedInstanceState) {
....
timetableAdapter = new TimetableAdapter(this, getModel());
}
private List<Lesson> getModel() {
return timetable.getLessons();
}
public void onTabChanged(String tabId) {
currentTabName = tabId;
if (tabId.equals("tabMonday")) {
setCurrentListView(mondayListView);
}
else if (tabId.equals("tabTuesday")) {
// Checks tabTuesday and so on....
...
}
}
private void addLesson() {
timetable.addLesson(new Lesson(blabla(name, weekday etc.))); // blabla = user specified values
timetableAdapter.notifyDataSetChanged();
}
基本的に、ユーザーがレッスンを追加すると、対応する平日、名前などのパラメータを指定します。これはblablaで表されます。
問題は、自分のデータにArrayListを1つしか使用していないため、月曜日や火曜日のレッスンなどです。私の火曜日のListViewに空の行として表示されます。getView(...)
がlessonListの各項目に対して呼び出され、平日が希望のものでない場合はnew View()
を返します。
1つの解決策は、適切な平日のために5つのArrayListsと5つのArrayAdapterを作成している可能性があります。したがって、月曜日のレッスンはArrayList mondayList
になり、アダプターはこのリストにバインドされます。しかし、これはやや柔軟性がありません。 もっと良い解決策はありますか?
ありがとうございます。
ありがとうございました。その解決法は実装が比較的容易であると思われる。私はそれを試してみましょう。 – r6203
ところで、オブジェクトの深いコピーではなく、参照をコピーするだけです。 –