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

问题描述

我想impement学校规划师的应用程序。这里是每天裹成一个选项卡布一个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();
}

所以基本上,如果用户增加了一个教训,他像指定对应的工作日,名称等,这一些参数进行了重新$ P $由布拉布拉psented。

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(...)被要求在lessonList每个项目,它只是返回新视图(),如果工作日是不希望的,我想。

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 ArrayAdapters的拨款平日。因此,在周一的经验教训将在的ArrayList mondayList ,和适配器将被绑定到这个名单。但是,这是有点unflexible。
有没有更好的解决办法?

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中走出适配器,建立某种形式的滤波方法来复制适用对象,并通过这个新的列表到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只显示特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:22