在哪里调用ArrayAdapter的过滤器?

时间:2014-01-22 16:02:47

标签: android android-listview android-arrayadapter android-listfragment android-filterable

我正在制作一个家庭作业计划器应用程序,我正在寻找一种方法来只显示包含Task对象的ArrayList中的某些元素。用户单击课程标题列表中的课程后,应显示与该课程相关的任务列表。目前,无论选择哪个课程,它都会显示所有任务的列表。每个Task对象在名为mBelongsToCourse的字段中存储它所属的过程。我在TaskAdapter中创建了一个Filter来按过程名称进行过滤,但我不知道在哪里调用它来使其工作。我在这里广泛搜索了这个问题的答案,但是在某些人过滤ArrayAdapter的情况下,似乎过滤器正在响应某些用户输入,例如搜索。在我的例子中,当用户选择课程时,用户输入只是课程名称的额外意图,它开始显示我的TaskListFragment的意图,但是应该只显示该课程的任务。

下面是我的TaskListFragment类,其中包括TaskAdapter和TaskFilter:

public class TaskListFragment extends ListFragment {

private ArrayList<Task> mTasks;
private  static String courseName;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    courseName = getActivity().getIntent().getStringExtra("name");
    getActivity().setTitle(courseName);
    mTasks = TaskLab.get(getActivity()).getTasks();

    TaskAdapter adapter = new TaskAdapter(mTasks);
    setListAdapter(adapter);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    //Get the Task from the adapter
    Task t = ((TaskAdapter)getListAdapter()).getItem(position);
    // Start TaskActivity with this task
    //Intent i = new Intent(getActivity(), TaskPagerActivity.class);
    Intent i = new Intent(getActivity(), TaskActivity.class);
    i.putExtra(TaskFragment.EXTRA_TASK_ID, t.getId());
    startActivity(i);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.fragment_task_list, menu);
} 

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_item_new_task:
        Task task = new Task();
        task.setBelongsToCourse(courseName);
        Log.d("yoyo", "belongsToCourse was set as: " + task.getBelongsToCourse());
        TaskLab.get(getActivity()).addTask(task);
        Intent i = new Intent(getActivity(), TaskActivity.class);
        i.putExtra(TaskFragment.EXTRA_TASK_ID, task.getId());
        startActivityForResult(i, 0);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    getActivity().getMenuInflater().inflate(R.menu.task_list_item_context, menu);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
        Bundle savedInstanceState) {
    View v = super.onCreateView(inflater, parent, savedInstanceState);
    ListView listView = (ListView)v.findViewById(android.R.id.list);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

        public void onItemCheckedStateChanged(ActionMode mode, int position,
                long id, boolean checked) {
            // Required, but not used in this implementation
        }

        // ActionMode.Callback methods
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.task_list_item_context, menu);
            return true;
        }

        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
            // Required, but not used in this implementation
        }
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
            case R.id.menu_item_delete_task:
                TaskAdapter adapter = (TaskAdapter)getListAdapter();
                TaskLab taskLab = TaskLab.get(getActivity());
                for (int i = adapter.getCount() - 1; i >= 0; i--) {
                    if (getListView().isItemChecked(i)) {
                        taskLab.deleteTask(adapter.getItem(i));
                    }
                }
                mode.finish();
                adapter.notifyDataSetChanged();
                return true;
            default:
                return false;
            }
        }

        public void onDestroyActionMode(ActionMode mode) {
            // Required, but not used in this implementation
        }
    });
    return v;
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
    int position = info.position;
    TaskAdapter adapter = (TaskAdapter)getListAdapter();
    Task task = adapter.getItem(position);

    switch (item.getItemId()) {
    case R.id.menu_item_delete_task:
        TaskLab.get(getActivity()).deleteTask(task);
        adapter.notifyDataSetChanged();
        return true;
    }
    return super.onContextItemSelected(item);
}


@Override
public void onResume() {
    super.onResume();
    ((TaskAdapter)getListAdapter()).notifyDataSetChanged();
}

private class TaskAdapter extends ArrayAdapter<Task> implements Filterable {

    private ArrayList<Task> taskList;
    private Filter taskFilter;

    public TaskAdapter(ArrayList<Task> tasks) {
        super(getActivity(), 0, tasks);
        this.taskList = tasks;
        this.taskFilter = new TaskFilter();
        getFilter().filter(courseName);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // If we weren't given a view, inflate one
        if (convertView == null) {
            convertView = getActivity().getLayoutInflater()
                    .inflate(R.layout.list_item_task, null);
        }

        // Configure the view for this Task
        Task t = getItem(position);

        TextView titleTextView =
                (TextView)convertView.findViewById(R.id.task_list_item_titleTextView);
        titleTextView.setText(t.getTitle());
        TextView dateTextView =
                (TextView)convertView.findViewById(R.id.task_list_item_dateTextView);
        dateTextView.setText(t.getDate().toString());
        CheckBox completedCheckBox =
                (CheckBox)convertView.findViewById(R.id.task_list_item_completedCheckBox);
        completedCheckBox.setChecked(t.isCompleted());
        return convertView;
    }

    @Override
    public Filter getFilter() {
        if (taskFilter == null)
            taskFilter = new TaskFilter();
        return taskFilter;
    }

    private class TaskFilter extends Filter {

        @Override
        protected FilterResults performFiltering (CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint == null | constraint.length() == 0) {
                results.values = taskList;
                results.count = taskList.size();
            } else {
                ArrayList<Task> newTaskList = new ArrayList<Task>();
                for (Task t : taskList) {
                    if (t.getBelongsToCourse().toUpperCase().equals(constraint.toString().toUpperCase())) {
                        newTaskList.add(t);
                    }
                }
                results.values = newTaskList;
                results.count = newTaskList.size();
            } return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {

            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0)
                notifyDataSetInvalidated();
            else {
                taskList = (ArrayList<Task>)results.values;
                notifyDataSetChanged();
            }
        }
    }
}
}

任何帮助将不胜感激!我一直试图解决这个问题太久了,我还是完全迷失了。

编辑:我已经完成了与原始Filter相关的一切,而是在TaskAdapter中使用此方法:

public ArrayList<Task> filterTasks() {
     ArrayList<Task> filteredTasks = new ArrayList<Task>();
     for (Task t: taskList) {
          if (t.getBelongsToCourse().equals(courseName)) {
               filteredTasks.add(t);
          }
     } return filteredTasks;
}

过滤任务。在TaskListFragment的onCreate方法中,我添加了下面的中间行:

TaskAdapter adapter = new TaskAdapter(mTasks);
adapter.filterTasks();
setListAdapter(adapter);

我还使LogCat打印出filteredTasks中的任务列表(仍然在onCreate方法中)。因此,当我单击一个课程,添加一个任务,然后返回到TaskListFragment时,我会打印出属于该课程的任务。每个课程的名单都是分开的!到目前为止,这对我来说是一个很大的进步,因为在过去的一周里我一直试图这样做并且看不到任何结果。尽管如此,我仍然看到每个课程页面上的所有任务,而不是过滤的任务。这只是我的listView不能更新的问题吗?

1 个答案:

答案 0 :(得分:0)

在onCreate方法中,您将上一个Activity的所有任务传递给您的Adapter,然后将其设置为ListView,为什么不在将任务传递给适配器之前过滤它们?繁重的工作应该在一个不同的线程中完成,以避免阻止UI太长时间,当你开始初始化List时显示progressDialog,当它被过滤后隐藏它。

我的意思是如果你给ListView充气,用它包含所有任务的列表设置它的适配器,如果你想在初始化之后修改List你应该在设置适配器之前完成它,以避免在其上添加额外的项目。如果您仍想这样做,请将过滤器应用于任务列表,然后调用notifyDataSetChanged()以使适配器更改项目。恕我直言我会在将任务列表传递给适配器之前更改它以避免这种情况。如果你有UI冻结使用不同的线程。