我正在使用ListFragment
(不是来自支持库)。我有SimpleCursorAdapter
,我正在使用android.R.layout.simple_list_item_2
。数据显示完全正常,但我没有在onListItemClick
方法中收到任何内容。
我试图实现自己的自定义适配器,扩展CursorAdapter
。同样,数据显示正常,但没有触发任何点击事件。
接下来,我尝试从here复制默认ListFragment
布局的来源。再一次,没有骰子。
再次,我正在使用android.R.layout.simple_list_item_2
。我还尝试了simple_list_item_1
。两者都给了我一个问题,我以前从来没有遇到过这些问题。
我的目标是API 15+,因此我没有使用支持库。
我还应该尝试做些什么呢?
修改 ListFragment
实施代码。其中大部分是由Android Studio自动生成的。自动生成的代码包含支持库,我将其更改为标准包。
import android.app.Activity;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import com.parse.ParseUser;
import com.blah.adapters.SuggestionCursorAdapter;
import com.blah.models.DateActivity;
import com.blah.provider.Contracts;
import com.blah.provider.UpdateService;
public class DateActivityListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String TAG = DateActivityListFragment.class.getSimpleName();
/**
* The serialization (saved instance state) Bundle key representing the
* activated item position. Only used on tablets.
*/
private static final String STATE_ACTIVATED_POSITION = "activated_position";
/**
* The fragment's current callback object, which is notified of list item
* clicks.
*/
private Callbacks mCallbacks = sDummyCallbacks;
/**
* The current activated item position. Only used on tablets.
*/
private int mActivatedPosition = ListView.INVALID_POSITION;
private SuggestionCursorAdapter mAdapter;
/**
* A callback interface that all activities containing this fragment must
* implement. This mechanism allows activities to be notified of item
* selections.
*/
public interface Callbacks {
/**
* Callback for when an item has been selected.
*/
public void onItemSelected(DateActivity dateActivity);
/**
* Callback for when a new item should be created.
*/
public void onNewItem();
}
/**
* A dummy implementation of the {@link Callbacks} interface that does
* nothing. Used only when this fragment is not attached to an activity.
*/
private static Callbacks sDummyCallbacks = new Callbacks() {
@Override
public void onItemSelected(DateActivity dateActivity) {
}
@Override
public void onNewItem() {
}
};
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public DateActivityListFragment() {
}
final static String[] FROM = {Contracts.DateActivities.C_NAME, Contracts.DateActivities.C_DESCRIPTION};
final static int[] TO = {android.R.id.text1, android.R.id.text2};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new SuggestionCursorAdapter(getActivity(), null);
// mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2, null, FROM, TO, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
registerForContextMenu(getListView());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dateactivity_list, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Restore the previously serialized activated item position.
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks.
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException("Activity must implement fragment's callbacks.");
}
mCallbacks = (Callbacks) activity;
}
/////////////// OPTIONS MENU
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.suggestion, menu);
final ParseUser parseUser = ParseUser.getCurrentUser();
// Only show add button for dev user
if (parseUser != null && !parseUser.getUsername().equals("dev")) {
menu.findItem(R.id.menu_add).setVisible(false);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_add) {
mCallbacks.onNewItem();
return true;
}
if (id == R.id.menu_refresh) {
UpdateService.startActionUpdate(getActivity());
return true;
}
return super.onOptionsItemSelected(item);
}
/////////////// END OPTIONS MENU
/////////////// CONTEXT MENU
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
final MenuInflater menuInflater = getActivity().getMenuInflater();
menuInflater.inflate(R.menu.suggestion_context, menu);
// Set menu title
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
final Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
menu.setHeaderTitle(cursor.getString(Contracts.DateActivities.I_NAME));
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_delete) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
// TODO promopt, are you sure first
// delete from service.
UpdateService.startActionDeleteItem(getActivity(), new DateActivity(cursor),
cursor.getLong(Contracts.DateActivities.I_ID));
return true;
}
return super.onContextItemSelected(item);
}
/////////////// END CONTEXT MENU
@Override
public void onDetach() {
super.onDetach();
// Reset the active callbacks interface to the dummy implementation.
mCallbacks = sDummyCallbacks;
}
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
Log.v(TAG, "onListItemClick");
super.onListItemClick(listView, view, position, id);
Cursor c = ((CursorAdapter) listView.getAdapter()).getCursor();
c.moveToPosition(position);
mCallbacks.onItemSelected(new DateActivity(c));
c.close();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION) {
// Serialize and persist the activated item position.
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
}
/**
* Turns on activate-on-click mode. When this mode is on, list items will be
* given the 'activated' state when touched.
*/
public void setActivateOnItemClick(boolean activateOnItemClick) {
// When setting CHOICE_MODE_SINGLE, ListView will automatically
// give items the 'activated' state when touched.
getListView().setChoiceMode(activateOnItemClick
? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}
private void setActivatedPosition(int position) {
if (position == ListView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, false);
} else {
getListView().setItemChecked(position, true);
}
mActivatedPosition = position;
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
final Uri baseUri = Contracts.DateActivities.CONTENT_URI;
return new CursorLoader(getActivity(), baseUri,
Contracts.DateActivities.SELECT_ALL_PROJECTION, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
mAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
mAdapter.swapCursor(null);
}
}
答案 0 :(得分:1)
将父活动更改为常规活动,而不是ListActivity。
答案 1 :(得分:-1)
以上回答 - 将家长活动从ListActivity
更改为Activity
- 这对我有用