开发者

ArrayAdapter and ListView display only specific items

开发者 https://www.devze.com 2023-04-10 08:32 出处:网络
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 g

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 开发者_StackOverflow社区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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号