버튼이 들어있는 사용자 지정 목록 엔트리 레이아웃을 생성하기 위해 SimpleCursorAdapter를 서브 클래 싱했습니다. 버튼을 로그에 쓰는 것까지 갈 수 있습니다. 나의 의도는 AlertDialog를 시작하는 것이지만 FragmentManager가 DialogFragment를 시작하게하는 방법을 찾지 못했습니다. 조각 호환성 구성 요소를 사용하고 있습니다.서브 클래 싱 된 SimpleCursorAdapter에서 AlertDialog 시작하기
이 경우 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();
}
}
감사를 여기
어댑터입니다 당신.해결 된 답변 매우 간단한 해결책이 있습니다. 조각의 구성 요소를 로컬에서 참조 할 수 있도록 어댑터를 조각 작업으로 옮겼습니다.
CursorAdapter 내에서 getFragmentManager를 호출하려면 어떻게해야합니까? – KrisC
또는 getFragmentManager를 호출 할 수 있도록 활동에서 응답하도록 사용자 정의 목록 입력 레이아웃에서 클릭을 얻는 방법. – KrisC
어댑터의 getView에서 리스너 inliine을 선언합니다. getFragmentManager는 어댑터 안에 있으므로 getFragmentManager에 액세스 할 수 없습니다. 아마도 나는 뭔가를 놓치고 있습니다. – KrisC