سؤال

I'm using a ListFragment (Not from support library). I have a SimpleCursorAdapter and I'm using android.R.layout.simple_list_item_2. Data shows up perfectly fine however I'm not receiving anything in the onListItemClick method.

I've tried to implement my own custom adapter which extends CursorAdapter. Again, data shows up fine however no click event is fired.

Next I tried to copy the source for the default ListFragment layout from here. Again, no dice.

Again, i'm using android.R.layout.simple_list_item_2. I also attempted simple_list_item_1. Both are giving me an issue, i've never had problems with them before.

I'm targeting API 15+ so I'm not using the support library.

What else should I try to get this working?

Edit Code for ListFragment implementation. Most of which was auto-generated by Android Studio. The auto-generated code contained the support library, which I changed to a standard package.

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);
    }
}
هل كانت مفيدة؟

المحلول

Change the parent activity to a regular Activity, not a ListActivity.

نصائح أخرى

Above answer - change parent activity from ListActivity to Activity - it worked for me

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top