I try to select an item in ExpandableListView using setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup), but nothing happens.
Here's the primitive activity I tested it with:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class StartupActivity extends Activity {
private static final String[] _items = new String[] {"Oops", "Wham", "Bam"};
private ExpandableListView _explist;
private TextView _txt_hello;
private Button _btn_sel;
private MyExplistAdapter _explist_adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_explist = (ExpandableListView) findViewById(R.id.explistMain);
_txt_hello = (TextView) findViewById(R.id.txtHello);
_btn_sel = (Button) findViewById(R.id.btnAction);
_btn_sel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
selectElement();
}
});
}
@Override
protected void onResume() {
super.onResume();
_explist_adapter = new MyExplistAdapter();
_explist.setAdapter(_explist_adapter);
_explist.expandGroup(0);
}
private void selectElement() {
boolean success = _explist.setSelectedChild(0, 1, true);
long sel = _explist.getSelectedId();
_txt_hello.setText((success ? "success" : "failure") + " " + sel);
//setSelectedChild() returns true
//but getSelectedId() returns -1 and nothing is actually selected
}
public class MyExplistAdapter extends BaseExpandableListAdapter {
@Override
public Object getChild(int groupPosition, int childPosition) {
return _items[childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new TextView(StartupActivity.this);
}
TextView txt_view = (TextView) convertView;
txt_view.setText(_items[childPosition]);
txt_view.setHeight(50);
return txt_view;
}
@Override
public int getChildrenCount(int groupPosition) {
return _items.length;
}
@Override
public Object getGroup(int groupPosition) {
return getGroupId(groupPosition);
}
@Override
public int getGroupCount() {
return 1;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView txt_view = new TextView(StartupActivity.this);
txt_view.setBackgroundColor(Color.GRAY);
txt_view.setHeight(50);
return txt_view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
The layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello"
android:id="@+id/txtHello" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnAction"
android:text="Select"></Button>
<ExpandableListView android:layout_width="match_parent"
android:id="@+id/explistMain" android:layout_height="fill_parent"></ExpandableListView>
</LinearLayout>
How can I make it work?