3

I am trying to set item selected in OnItemClick event in ListView and it just wouldn't leave item selected. What am I doing wrong?

lView.setOnItemClickListener(new OnItemClickListener()
   {
    @Override
    public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View clickedview, int position, long id)
    {
     clickedview.setSelected(true); 
        mItemsAdapter.select(position);
    }
   }); 

few things:
1. I am trying to implement Multiple Select on the list View.
2. I cannot extend from ListActivity because Activity extends from BaseActivity custom class already.
3. mItemsAdapter is a custom ItemsAdapter adapter that extends BaseAdapter.
4. I don't need a checkbox in there, just to be able to see the row selected is fine.
5. ItemsAdapter overrides getView() and sets the layout of the row by inflating xml

dropsOfJupiter
  • 6,763
  • 12
  • 47
  • 59

3 Answers3

5

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().

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
Leandro
  • 63
  • 1
  • 4
0

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).

sgarman
  • 6,152
  • 5
  • 40
  • 44
  • thank you, I've seen the checkedtextview implementation somewhere and will try it as well although I would need some code. You can even do this with OnTouchEvent it just seems like an overkill. Shouldn't there be something very simple? :) BTW checkedtextview is that a control or an xml file? I have my own xml file that contains somewhere from 4 to 6 controls in it and they are all dynamically turned on/off depending on what comes back in the object. Its not just plain TextView. A lot of stuff is done in the adapter getView() – dropsOfJupiter Jan 26 '11 at 17:41
0

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.

Community
  • 1
  • 1
Beasly
  • 1,517
  • 4
  • 20
  • 30
  • thank you I see the first post has a bit different code and I did encounter the same problem with item jump. What is that? I am going to try the method now. – dropsOfJupiter Jan 26 '11 at 17:37
  • @dropsOfJupiter OK try, in case you have problem tell me! – Beasly Jan 26 '11 at 18:02
  • @dropsOfJupiter The "problem" is that each row (view) which gets scrolled out the list gets reused for the row which gets displayed instead. This has to be handled and the references of the "new" view (row) need to get updated. For some reason the normal `onClick()` method doen't work then anymore – Beasly Jan 27 '11 at 07:12