android - 从子类 SimpleCursorAdapter 启动 AlertDialog

标签 android android-fragments simplecursoradapter onclicklistener

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

对于这种情况,我意识到可能有更简单的解决方案可以避免使用 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;
    }
}

这是调用适配器的 fragment :

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);
    }
}

这是包含 fragment 的 Activity :

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();
    }
}

谢谢。

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

最佳答案

在 fragment 内定义适配器,以便它可以访问 Activity 的资源。

关于android - 从子类 SimpleCursorAdapter 启动 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12989264/

相关文章:

android - 以编程方式为 TTS 设置语言?

android - 在Navigation-Drawer的抽屉中加载自定义fragment,并在主布局中打开对应的fragment

android - 从 Activity 调用 DialogFragment 导致 "IllegalStateException: fragment not attached to Activity"

android - 自定义 SimpleCursorAdapter

java - 如何将日期转换成不同的格式

java - 滑动 Android 时通知不会消失

Android,通过使用 SimpleCursorAdapter.ViewBinder 将 URL 解析为参数来检索缩略图

android - ListView SimpleCursorAdapter 更新

android - 使用 RectangleF 时 Xamarin.forms 错误

android - 在 Fragment 中使用 smoothScrollTo