即使在调用重启加载程序后,listview也不会刷新

时间:2012-12-06 06:04:18

标签: android android-listfragment android-loadermanager

长按列表项我有上传菜单的List片段。单击列表项并从我的数据库中删除列表项。但是即使在调用重启加载程序之后列表也不会刷新?

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setEmptyText("Loading...");

        myAdapter = new MySimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.my_list_item, null,  new String[] {DBConstants.NAME,DBConstants.ITEM1,DBConstants.SYMBOL}, new int[] {R.id.my_companyname,R.id.my_item1,R.id.my_symbol},0);
        setListAdapter(myAdapter);
        registerForContextMenu(getListView());
        MyLoader = getActivity().getSupportLoaderManager().initLoader(1, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        DBHelper dbHelper=new DBHelper(getActivity());
        return new MyLoader(getActivity(),dbHelper);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
        if(c!=null && c.getCount()>0){
            myAdapter.swapCursor(c);
            if (isResumed()) {
                setListShown(true);
            } else {
                setListShownNoAnimation(true);
            }
        }
        else
        {
            setEmptyText("Not selected");
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        myAdapter.swapCursor(null);
    }

@Override
    public boolean onContextItemSelected(MenuItem item) {
        String selectedItem = item.getTitle().toString();
        Log.i("context", "selected context menu item->"+selectedItem);

        if(selectedItem.equalsIgnoreCase("Remove"))
        {
            AdapterView.AdapterContextMenuInfo menuInfo=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
            String[] args={String.valueOf(menuInfo.id)};
            Log.i("context", String.valueOf(menuInfo.id));
            new DBHelper(getActivity()).getWritableDatabase().delete(DBConstants.MYSTOCKS_TABLE, "_ID=?", args);

            getActivity().getSupportLoaderManager().restartLoader(1, null, this);

            /*
             Cursor cursor = myAdapter.getCursor();
            myAdapter.swapCursor(cursor);
            */
            //getLoaderManager().initLoader(0, null, this);
        }
        return super.onContextItemSelected(item);
    }

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

    }

1 个答案:

答案 0 :(得分:0)

您是否在活动的onCreate或onResume中添加了片段?如果是后者,您可能希望将片段设置(事务创建,片段添加,提交)移动到onCreate。完成后,restartLoader()将正常工作。