2

为了创建包含按钮的自定义列表条目布局,我将 SimpleCursorAdapter 子类化。我可以让按钮写入日志。我的意图是启动一个 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();
    }
}

谢谢你。

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

4

2 回答 2

2

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

于 2012-10-22T21:18:22.363 回答
0

它实际上很简单,只需创建对话框片段类,在按钮onClick方法中调用它,然后使用show带有getFragmentManager()“对话框”关键字的方法......

YourDialogFragment dialogFragment = new YourDialogFragment();
dialogFragment.show(getFragmentManager(), "dialog");
于 2012-10-20T14:24:57.623 回答