ArrayAdapter and ListView display only specific items

左心房为你撑大大i 提交于 2020-01-06 19:42:11

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7613495/arrayadapter-and-listview-display-only-specific-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!