ArrayAdapter 和 ListView 只显示特定项 [英] ArrayAdapter and ListView display only specific items

查看:30
本文介绍了ArrayAdapter 和 ListView 只显示特定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用学校规划器应用程序.有一个时间表概览实现为包含在选项卡布局中的每一天的 ListView.因此,用户可以在周一到周五之间切换,并获取特定日期的时间表.

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

因此,基本上如果用户添加课程,他会指定一些参数,例如相应的工作日、名称等.这由 blabla 表示.

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

问题是,因为我只使用一个 ArrayList 作为我的数据,无论它是星期一还是星期二的课程,例如星期一在我星期二的 ListView 上呈现为一个空行,因为 getView(...) 为课程列表中的每个项目调用,它只返回一个 new View(),如果工作日不是想要的那一天,我想.

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.

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

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?

提前致谢.

推荐答案

不要使用 ArrayAdapter,而是使用 SimpleAdapter.对于您想要显示的内容,它要灵活得多.其次,将所有约会的 ArrayList 保留在 Adapter 之外,创建某种过滤方法来复制适用的对象并将这个新列表传递给 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.

这篇关于ArrayAdapter 和 ListView 只显示特定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆