listfragment 长短按item问题

ListFragment 必须实现侦听器:

public class MessageListFragment extends SherlockListFragment
    implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemLongClickListener

    private static final int DELETE_ID = Menu.FIRST + 1;    

    private SimpleCursorAdapter adapter;

    // The LoaderManager needs initializing
    @Override
    public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       // Fields from the database (projection)
       // Must include the _id column for the adapter to work
       String[] from = new String[] { BookmarksTable.BOOKMARK_NAME, 
            BookmarksTable.BOOKMARK_PHONE_NAME };
       // Fields on the UI to which we map
       int[] to = new int[] { R.id.titleText, R.id.phoneText };

       // connect to the database
       getLoaderManager().initLoader(0, null, this);
       adapter = new BookmarkCursorAdapter(getActivity(), 
            R.layout.bookmark_row, null, from, to, 0);

       setListAdapter(adapter);
    }

    // register to put up the context menu
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
       View root = super.onCreateView(inflater, container, savedInstanceState); 
       registerForContextMenu(root);
       return root;
    }

    // set the listeners for long click
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        getListView().setOnItemLongClickListener(this);
    }

调用的方法是:

 /**
  * Called when a message is clicked.
  */
 @Override
 public void onListItemClick(ListView parent, View view, int position, long id) {
        // do item click stuff; show detailed view in my case

 }


@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    return false; // let the system show the context menu
  }

// Context menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}

    // respond to the context menu tap
@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
    switch (item.getItemId()) {
    case DELETE_ID:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                .getMenuInfo();
        Uri uri = Uri.parse(DatabaseContentProvider.BOOKMARK_ID_URI + Long.toString(info.id));
        getActivity().getContentResolver().delete(uri, null, null);
        return true;
    }
    return super.onContextItemSelected(item);
}

为完整起见,这里是加载程序代码

// Loader code
// Creates a new loader after the initLoader () call
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = { BookmarksTable.KEY_ID, BookmarksTable.BOOKMARK_NAME, BookmarksTable.BOOKMARK_PHONE_NAME };
    CursorLoader cursorLoader = new CursorLoader(getActivity(),
            DatabaseContentProvider.BOOKMARKS_URI, projection, null, null, null);
    return cursorLoader;
}

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

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    // data is not available anymore, delete reference
    adapter.swapCursor(null);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值