0

I have a SeperatedListAdapter with 2 sections and each section having 6 items in it.

Code:

listView.setAdapter(adapter);
listView.setOnItemClickListener(listViewListener);

Adding section headers and items in this fashion:

adapter = new SeparatedListAdapter(this);
adapter.addSection(entry.getKey(), new ItemAdapter(this, 0, topics.toArray(array)));

OnItemClickListener listViewListener = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long duration) {
          Employee emp = emps.get(position - 1); 
    }
};

I have ArrayList as:

items from section 1
    Anand - 0
    Sunil - 1
    Suresh - 2
    Dev - 3
    Faran - 4
    Khan - 5
items from section 2
    Samba - 6
    Surendra - 7
    Rajesh - 9
    Rakesh - 10
    Satish - 11

Now in OnItemClickListener when I get the position, it's also taking the section header as position.

So I did it as Employee emp = emps.get(position - 1); but up to 6 items (0-5 from my arraylist) is fine but after that the position is not proper. How can I solve this issue?

I need to pass the postion to my Arrray list in this fashion

Employee emp = emps.get(position - 1);

since I will be passing the employee object to another class.

see this too:

Android - SeparatedListAdapter - How to get accurate item position on onClick?

Community
  • 1
  • 1
Goofy
  • 6,098
  • 17
  • 90
  • 156

1 Answers1

1

As you mensiond in you comment you are using the Separating Lists with Headers in Android 0.9 example.

So there is a method into adpater,

public Object getItem(int position) {  
        for(Object section : this.sections.keySet()) {  
            Adapter adapter = sections.get(section);  
            int size = adapter.getCount() + 1;  

            // check if position inside this section   
            if(position == 0) return section;  
            if(position < size) return adapter.getItem(position - 1);  

            // otherwise jump into next section  
            position -= size;  
        }  
        return null;  
    }  

which returns the correct item.

So you only need to call this method, into OnItemClickListener like

OnItemClickListener listViewListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long duration) {
            Employee emp = (Employee) adapter.getItem(position); // HERE is the code to get correct item.
}
};

Add below method to SeparatedListAdapter

public Employee getItem(int position, ArrayList<Employee> lists) {  
        for(Object section : this.sections.keySet()) {  
            Adapter adapter = sections.get(section);  
            int size = adapter.getCount() + 1;  

            // check if position inside this section   
            if(position == 0) return lists.get(position);   
            if(position < size) return lists.get(position - 1);  

            // otherwise jump into next section  
            position -= size;  
        }  
        return null;  
    }  

and call it as

Employee emp = adapter.getItem(position, emps);
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • yes i had tried that it says String cannot cast to my Employee – Goofy Oct 04 '13 at 05:52
  • Yes thats correct. If you are giving String array to the adapter then the given method return the object which will be typecast-able to String not Employee. to get employee object you need to pass Employee array to Adapter. – Pankaj Kumar Oct 04 '13 at 05:55
  • this is how i am setting the section and the items adapter.addSection(entry.getKey(), new ItemAdapter(this, 0, topics.toArray(array))); – Goofy Oct 04 '13 at 05:58
  • What is the type of `topics.toArray(array))`? String or Employee array? If Employee, then you should modify getItem of `ItemAdapter` adapter. If not there add this method into the class. – Pankaj Kumar Oct 04 '13 at 06:02
  • Its a string ArrayList topics = entry.getValue(); – Goofy Oct 04 '13 at 06:05
  • Then you can not get Employee object from the method. Are you able to access `emps` list into SeparatedListAdapter? – Pankaj Kumar Oct 04 '13 at 06:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38591/discussion-between-goofy-and-pankaj-kumar) – Goofy Oct 04 '13 at 06:11
  • Buddy i have one more problem after solving this issue , can you please help me out – Goofy Oct 04 '13 at 09:09
  • dude if you are free can you please have a look at this http://stackoverflow.com/questions/19264300/bitmap-with-tile-mode-repeat-and-round-corners – Goofy Oct 09 '13 at 06:47
  • thats ok, i have solved it, i have answered my own questions so that it will help some one in future.Just have a look at it http://stackoverflow.com/questions/19264300/bitmap-with-tile-mode-repeat-and-round-corners/19268915#19268915 – Goofy Oct 09 '13 at 10:05