从子类SimpleCursorAdapter启动AlertDialog

时间:2012-10-20 14:14:30

标签: android android-fragments simplecursoradapter onclicklistener

我已经将SimpleCursorAdapter子类化,以便创建包含Button的自定义列表条目布局。我可以使按钮写入日志。我的目的是启动一个AlertDialog,但我还没有找到一种方法让FragmentManager启动DialogFragment。我正在使用片段兼容性组件。

对于这种情况,我意识到可能有更简单的解决方案,避免使用SimpleCursorAdapter,但我试图在处理更复杂的组件之前学习这种情况。

这是适配器:

public class ActiveProfileAdapter extends SimpleCursorAdapter {
    private static final String DEBUG_TAG = "ActiveProfileAdapter";
    private final Cursor dataCursor;
    private final LayoutInflater mInflater;

    public ActiveProfileAdapter(final Context context, final int layout, final Cursor dataCursor, final String[] from,
            final int[] to, final int flags) {
        super(context, layout, dataCursor, from, to, flags);
        this.dataCursor = dataCursor;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.profilebar_list_item, null);

            holder = new ViewHolder();
            holder.button1 = (Button) convertView.findViewById(R.id.currentprofile);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        dataCursor.moveToPosition(position);
        final int label_index = dataCursor.getColumnIndex(ProfilesColumns.USERNAME);
        if (label_index == -1) {
            Log.d(DEBUG_TAG, "Bad column name");
        }

        final String label = dataCursor.getString(label_index);
        holder.button1.setText(label);
        holder.button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                Log.d(DEBUG_TAG, "Launch AlertDialog Fragment when this button is pressed");
            }
        });

        return convertView;
    }

    static class ViewHolder {
        Button button1;
    }
}

这是调用适配器的片段:

public class ActiveProfileFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
    private static final int LIST_LOADER = R.loader.browsefragloader;
    protected int layout = R.layout.profilebar_fragment;
    protected SimpleCursorAdapter listAdapter;

    protected final Uri table = ProfileProvider.URI_ACTIVEPROFILEVIEW;
    protected String[] uiBindFrom = { ProfilesColumns.USERNAME };
    protected String[] createProjection = { CommonDatabaseHelper._ID, ProfilesColumns.USERNAME };
    protected int[] uiBindTo = { R.id.currentprofile };

    // layout for list item
    protected int entryLayout = R.layout.profilebar_list_item;

    Button openProfile;

    public ActiveProfileFragment() {
        super();
    }

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Cursor c = getActivity().getContentResolver().query(table, createProjection, null, null, null);
        listAdapter = new ActiveProfileAdapter(getActivity().getApplicationContext(), entryLayout, c, uiBindFrom,
                uiBindTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        getLoaderManager().initLoader(LIST_LOADER, null, this);
        setListAdapter(listAdapter);
    }

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

    @Override
    public Loader<Cursor> onCreateLoader(final int id, final Bundle args) {
        final String[] projection = createProjection;
        final CursorLoader cursorLoader = new CursorLoader(getActivity(), table, projection, null, null, null);
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(final Loader<Cursor> loader, final Cursor cursor) {
        listAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(final Loader<Cursor> loader) {
        listAdapter.swapCursor(null);
    }
}

这是包含片段的活动:

public class BrowseActivity extends FragmentActivity {
    protected int layout = R.layout.browse_viewer;

    final Fragment profileBarFragment = new ActiveProfileFragment();
    final Fragment fragment0 = new BrowseFragment();

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout);
        final android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
        final android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.profilebarfragment, profileBarFragment);
        transaction.replace(R.id.fragment, fragment0);
        transaction.commit();
    }
}

谢谢。

解决 我找到了一个非常简单的解决方案。我将适配器移动到片段活动中,以便我可以在本地引用片段的组件。

2 个答案:

答案 0 :(得分:2)

在片段内定义适配器,以便它可以访问活动的资源。

答案 1 :(得分:0)

实际上非常简单,只需创建对话框片段类,在按钮onClick方法中调用它,然后使用show方法和getFragmentManager()以及“dialog”关键字。

YourDialogFragment dialogFragment = new YourDialogFragment();
dialogFragment.show(getFragmentManager(), "dialog");