Question

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?

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top