Android - setSelected in OnItemClick in ListView

我的未来我决定 提交于 2019-11-28 12:42:58
Beasly

I currently don't have much time. So I'll take a look again later this day. Anyway take a look at my previous questions, I was struggling with the same:

In case the solution for you is not in there (I think it's in the first one) we will need more code, in order to help you.

Hope this helps a bit.

Leandro

I could manage to get an item of a ListView to be set as selected when long-clicked:

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
        int position, long id) {
    parent.requestFocusFromTouch(); // IMPORTANT!
    parent.setSelection(position);
    return true;
}

It only worked after I called requestFocusFromTouch().

I have not had much luck using the built in functionality for selection.

I see you have your own custom adapter, which I am guessing means your inflating custom views as rows. If your row has anything more then a checkedtextview I don't think you will be able to correctly use setSelections.

I solved this problem by using my own models and functions. Each item in the list had data with it to determine it if was selected or not. I could then iterate though that array, toggle selections with and and even update the UI by changing values and calling notifydatasetchanged on the adapter (which used getView and checked against my selection model to draw checks).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!