使用onActivityResult返回时刷新listView

时间:2013-12-26 10:38:58

标签: android android-listview android-fragments onactivityresult

解释结构:在MainActivity中我有一个抽屉菜单和一个显示内容的片段。从抽屉我可以选择一个类别,该类别中的项目列表从数据库中读取,并显示在内容片段的listView中。

单击列表视图中的项目时,将启动DetailActivity。

问题:在DetailActivity中有一个按钮可以从数据库中删除该项目。按下此项时,DetailActivity将关闭。并显示MainActivity中的先前内容片段。 (比如按下后退按钮)但问题是我删除的项目仍然显示在listView中。我已经转到相关类别,以便listView刷新,该项目不再存在。

我的期望:我想要的是当我删除DetailActivity中的项目并返回上一个活动时,listView会自动更新。

我做了什么:我知道我可以像这样使用onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    // Refresh Bookmarks page when a bookmark is deleted
    if (requestCode == LESSON_DETAIL_ACTIVITY && resultCode == -1) {
        datasource.open();
        lessons = datasource.findMyLessons();
        refreshDisplay(context, view, category, i); // Problem Here!
    }
}

如您所见,我的refreshDisplay方法需要4个参数。最初这些参数被发送到DetailActivity。 (当在MainActivity中的片段中单击列表项时) 当我按下DetailActivity中的“删除项目”按钮并关闭时。我不知道如何检索这些参数,以便我可以刷新列表。

在这里,我发布了我的refreshDisplay方法的代码以及我如何调用它以防万一。 我在片段中调用了refreshDisplay方法(在MainActivity中),而refreshDisplay本身在MainActivity中。

public static class contentFragment extends Fragment {
    public static final String ARG_CATEGORY_NUMBER = "category_number";
    public contentFragment() {
        // Empty constructor required for fragment subclasses
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_content, container, false);
            int i = getArguments().getInt(ARG_CATEGORY_NUMBER);
            String category = getResources().getStringArray(R.array.category)[i];

            ((MainActivity) this.getActivity()).refreshDisplay(this.getActivity(),rootView, category, i);

            getActivity().setTitle(category);
            return rootView;
        }
}

我的refreshDisplay方法是:

public void refreshDisplay(Context context, View view, String category, int i) {

    List<Lesson> lessonByCategory = datasource.findByCategory(category, i);

    final ListView lv = (ListView) view.findViewById(R.id.listView);
    final ArrayAdapter<Lesson> adapter = new LessonListAdapter(context, lessonByCategory);
    lv.setAdapter(adapter);


    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
            Log.i(LOGTAG, "onListItemClick called");

            ArrayAdapter<Lesson> m_adapter = adapter;
            // get the Lesson object for the clicked row
            Lesson lesson = m_adapter.getItem(position);

            // use this if your `refreshDisplay()` method is in your activity
            Intent intent = new Intent(MainActivity.this, LessonDetailActivity.class);

            isStared = datasource.isStared(lesson);

            intent.putExtra(".model.Lesson", lesson);
            intent.putExtra("isStared", isStared);

            startActivityForResult(intent, LESSON_DETAIL_ACTIVITY);

        }
    });
    }

有人可以帮助我吗?

编辑1:在我的代码中,在 onActivityResult 中我调用了 refreshDisplay(context,view,category,i); 但请注意我传递的那些参数没有定义。没有正确的参数,refreshDisplay不起作用!

编辑2:我的抽屉导航包含类别列表。当我点击其中一个类别时,在我的内容片段中 onCreateView 调用 refreshDisplay **并放置上下文&amp; 查看参数以及类别&amp; i ,这是所选类别的名称和位置。现在 refreshDisplay 获取这些参数,并创建该类别的项目列表,并在内容片段中显示它。 refreshDisplay有一个列表适配器和侦听器,单击它时会打开 DetailActivity 。从 DetailActivity 我可以删除该项目,并关闭 DetailActivity 。我回到了清单。该列表仍显示已删除的项目。我想更新列表。

2 个答案:

答案 0 :(得分:0)

试试我的代码提示....我希望它可以解决你的问题

  1. 从适配器删除项目后调用此方法。它将刷新适配器内容。

    notifyDataSetChanged()

  2. 您可以将Context作为第一个参数传递给以下方法..

    refreshDisplay(/ * this.getActivity() / context,rootView,category,i);

答案 1 :(得分:0)

解决方案: -

  • 在Activity类(全局变量)中声明列表listCategory static
  • 将listView声明为活动类的实例变量。
  • 在DetailActivity中删除listCategory项(您可以访问它,因为它是静态的)     像这样: -

          MainActivity.listCategory.remove(<the position of that detail>);
    

  • 将您的适配器声明为您的活动类的实例变量。
  • 不需要你的refreshDisplay方法只需在onActivityResult()方法中使用adapter.notifyDataSetChanged()