Вопрос

I have a Fragment with ListView that implements LoaderManager.LoaderCallbacks and sometimes clicking on an list items does not trigger listener. I see clicked item highlighting but nothing more. It happens when onLoadFinished and onItemClick execute at the same time. Here is the code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.folder_list_fragment, container, false);
    listView = (ListView) view.findViewById(R.id.listView);
    listView.setOnItemClickListener(new ListViewOnItemClickListener());
}

private final class ListViewOnItemClickListener implements OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            System.out.println("on click");
            //load new data from the database and execute update asynch
        }
    }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    listView.setAdapter(adapter);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    adapter.swapCursor(data);
    System.out.println("swapCursor");
}

Log is:

swapCursor - first time initialising

on click - click on item
swapCursor - update data
swapCursor - update data

on click - click on item swapCursor - update data

click on item, see background highlighting like for normal click
swapCursor - here is the problem, I have clicked on an item, but notification from previous update has discarded onClick, no any print about click to the log

on click - next time clicking

Это было полезно?

Решение

Finally I have found where was a problem.

Project has a database with some information and INDEX column has name "id".

According this tutorial http://code.tutsplus.com/tutorials/android-sdk_content-providers--mobile-5549

A projection is simply a list of columns to use with the adapter. The ListView uses the titles and can provide an id when an item is clicked. For use with an adapter, the id column must be named "_id".

Before to resolve column name problem was implemented this PROJECTION:

public static final String[] DEFAULT_PROJECTION = new String[] {"id _id", FolderProvider.FOLDER_COLUMN_ID,
            FolderProvider.FOLDER_COLUMN_NAME}; 

So ContentProvider and CursorAdapter works correct but some time onItemClick() was discarded.

Renaming "id" to "_id" solve the problem.

Here is good LoaderManager.LoaderCallbacks tutorial http://code.tutsplus.com/tutorials/android-fundamentals-properly-loading-data--mobile-5673

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top