I have subclassed SimpleCursorAdapter in order to create a custom list entry layout containing a Button. I can get as far as making the button write to the log. My intent is to launch an AlertDialog, but I have yet to find a way to get a FragmentManager to launch the DialogFragment. I’m using the fragment compatibility components.
For this case I realize there may be simpler solutions which avoid the use of a SimpleCursorAdapter, but I am trying to learn this case before I work on more complicated components.
Here is the adapter:
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;
}
}
Here is the fragment which calls the adapter:
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);
}
}
And here is the activity which contains the fragment:
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();
}
}
Thank you.
Resolved
I hit upon a very simple solution. I moved the adapter into the fragment activity so that I could reference the fragment’s components locally.
Define the adapter inside the fragment so that it has access to the activity’s resources.