faizanjehangir
faizanjehangir

Reputation: 2831

Call setOnItemSelectedListener on expandable listview

I have an expandableListView, By default I want the first parentgroup i.e. position 0 to be selected. So that it calls my setOnItemSelectedListener. Here is how I am setting my adapter and select listener:

adapter = new ExpandableListAdapter(getApplicationContext(),
                lstEpisodes, list);
        list.setAdapter(adapter);

list.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(),
                        "focus on: " + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });

This is the xml layout of listview:

<LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ExpandableListView
                android:id="@+id/exlist"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:childDivider="#FFFFFF"
                android:choiceMode="singleChoice"
                android:divider="@android:color/transparent"
                android:groupIndicator="@null" >
                <requestFocus />
            </ExpandableListView>
        </LinearLayout>

This is what I have tried so far:

list.setSelection(0);
list.requestFocus();
list.invalidate();

How do I select the first row of the listview, a default selection when adapter is set..?

Upvotes: 1

Views: 485

Answers (1)

Justin Slade
Justin Slade

Reputation: 490

This is perhaps a hacky way to solve the problem but it will work. Capture your anonymous OnItemSelectedListener interface in a local variable assign it in the setOnItemSelectedListener method. Instead of setOnGroupClickListener you can use onGroupClick on the local variable. You have access to all of the variables needed to be passed in. This should be used as a absolute last resort.

Upvotes: 1

Related Questions