ArrayAdapter 和 ListView 仅显示特定项目

发布于 2024-12-07 04:40:35 字数 2148 浏览 0 评论 0原文

我正在尝试实施一个学校规划应用程序。有一个时间表概述作为每天的 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 来存储数据,所以无论是星期一还是星期二的主题,例如星期一的课程都会在星期二的 ListView 上呈现为空行,因为 getView(... ) 会为 LessonList 中的每个项目调用,并且它只返回一个 new View(),如果工作日不是所需的工作日,我认为。

一种解决方案可能是为适当的工作日创建 5 个 ArrayList 和 5 个 ArrayAdapter。因此,周一的课程将在 ArrayList mondayList 中,并且适配器将绑定到此列表。但这有些不灵活。 有更好的解决方案吗?

提前致谢。

I am trying to impement a school planner app. There is a timetable overview implemented as a ListView for each day wrapped into a Tab Layout. So the user can switch between days Monday to Friday and gets his timetable for the specific day.

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();
}

So basically if the user adds a lesson he specifies some parameters like corresponding weekday, name etc. This is represented by blabla.

The problem is that, because I am using only one ArrayList for my data, wether it's a subject on Monday or on Tuesday the lesson e.g. for Monday is rendered on my Tuesday's ListView as an empty row, because getView(...) is called for each item in lessonList and it just returns a new View(), if the weekday isn't the desired one, I think.

One solution might be creating 5 ArrayLists and 5 ArrayAdapters for the appropriated weekdays. So the lessons on Monday will be in ArrayList mondayList, and the adapter will be bound to this list. But this is somewhat unflexible.
Is there a better solution?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2024-12-14 04:40:35

不要使用 ArrayAdapter,而使用 SimpleAdapter。对于您想要显示的内容来说,它更加灵活。其次,将所有约会的 ArrayList 保留在适配器之外,创建某种过滤方法来复制适用的对象并将此新列表传递给 SimpleAdapter。

Instead of using ArrayAdapter, use SimpleAdapter. It is far more flexible for what you want to display. Second keep your ArrayList of all the appointments out of the Adapter, create some sort of filtering method to copy applicable objects and pass this new list to the SimpleAdapter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文