如何/在何处从另一个片段调用notifyDataSetChanged

时间:2013-04-18 09:18:28

标签: android android-contentprovider

我有一个带有listview的片段来显示我的列表内容。我通过另一个片段 - 对话框片段(使用内容提供者的删除和更新方法)更新和删除此列表中的项目,除了列表视图的“实时”刷新之外,一切正常。

我知道我应该在适配器上调用notifyDataSetChanged,但不知道在哪里。

由于用户通过不同的片段删除/更新,我猜我应该通过这个片段以某种方式调用适配器上的notifyDataSetChanged。我也调查了一下,我明白我应该在UI线程上运行它。

我尝试了几件事并没有成功。请看看我的代码。 这是包含ListView的片段:

  public class ShiftsFragment extends android.app.Fragment implements
        LoaderCallbacks<Cursor> {

    private Typeface tf, roboto;
    private ContentResolver cr;
    public shiftsAdapter sa = null;
    public static ShiftsDialog sd = null;

    public static ShiftsFragment newInstance(String title) {

        ShiftsFragment pageFragment = new ShiftsFragment();
        return pageFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        cr = getActivity().getBaseContext().getContentResolver();
        getLoaderManager().initLoader(0, null, this);

    }

    @Override
    public void onResume() {
        super.onResume();
        getLoaderManager().restartLoader(0, null, this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shifts, container, false);

        roboto = Typeface.createFromAsset(getActivity().getAssets(),
                "fonts/JuraLight.ttf");
        tf = Typeface.createFromAsset(getActivity().getAssets(),
                "fonts/Advert.ttf");
        ListView ls = (ListView) view.findViewById(android.R.id.list);

        sa = new shiftsAdapter(getActivity().getBaseContext(), null, 0);
        ls.setAdapter(sa);
        ls.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int lineNum, long arg3) {
                sd = new ShiftsDialog();
                sd.newInstnace(arg3);
                sd.show(getFragmentManager(), "shifts_dialog");
                return false;
            }
        });

        return view;

    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        CursorLoader loader = new CursorLoader(getActivity()
                .getApplicationContext(), MyProvider.SHIFTS_URI, null, null,
                null, null);

        return loader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {

        sa.swapCursor(cursor);
        Log.i("Shifts Numbers", "" + cursor.getCount());

    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {

    }


}

这是我根据用户点击的按钮更新/删除的对话框片段:

public class ShiftsDialog extends DialogFragment {
    private Typeface roboto;
    private static long position = 0;
    private ContentResolver cr = null;

    public static ShiftsDialog newInstnace(long i) {
        position = i;
        ShiftsDialog dial = new ShiftsDialog();

        return dial;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cr = getActivity().getBaseContext().getContentResolver();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));

        roboto = Typeface.createFromAsset(getActivity().getAssets(),
                "fonts/JuraLight.ttf");

        View view = inflater.inflate(R.layout.dialog, container, false);

        TextView tv = (TextView) view.findViewById(R.id.options);
        tv.setTypeface(roboto);

        Button delete = (Button) view.findViewById(R.id.deleteBtn);
        delete.setTypeface(roboto);

        // OnClick listener for delete function
        delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Complete it in the official way
                String where = String.valueOf(position);

                Uri rowURI = ContentUris.withAppendedId(MyProvider.SHIFTS_URI, position);
                cr.delete(rowURI, null, null);



            }
        });

        Button edit = (Button) view.findViewById(R.id.editBtn);
        edit.setTypeface(roboto);

        // OnClick listener for editing a shift
        edit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                EditDate_Dialog ed = new EditDate_Dialog();
                ed.newInstnace(position);
                ed.show(getFragmentManager(), "edit_dialog");
                dismiss();

            }
        });

        return view;

    }

}

我的ContentProiver的删除和更新方法(为简洁起见,省略了其余代码):

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = helper.getWritableDatabase();

        Log.d("URI", uri.toString());

        switch (uriMatcher.match(uri)) {
        case SINGLE_ROW:
            String rowID = uri.getPathSegments().get(1);
            selection = KEY_ID
                    + "="
                    + rowID
                    + (!TextUtils.isEmpty(selection) ? " AND (" + selection
                            + ')' : "");
        default:
            break;
        }

        if (selection == null)
            selection = "1";

        // Execute the deletion.
        int deleteCount = db.delete(helper.DATABASE_TABLE, selection,
                selectionArgs);
        Log.d("DeleteCount", "" + deleteCount);

        Log.i("Selection", "" + selection);

        getContext().getContentResolver().notifyChange(uri, null);

        return deleteCount;
    }

@Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        SQLiteDatabase db = helper.getWritableDatabase();

        switch (uriMatcher.match(uri)) {
        case SINGLE_ROW:
            String rowID = uri.getPathSegments().get(1);
            selection = KEY_ID
                    + "="
                    + rowID
                    + (!TextUtils.isEmpty(selection) ? " AND (" + selection
                            + ')' : "");

        default:
            break;
        }

        int updateCount = db.update(helper.DATABASE_TABLE, values, selection,
                selectionArgs);
        Log.d("Updated", "" + updateCount);
        getContext().getContentResolver().notifyChange(uri, null);

        return updateCount;
    }

0 个答案:

没有答案