ヘッダーと番号を含む展開可能なリストがあります。リスト内の番号がクリックされるたびに、ダイヤラを使用してアクションを完了したいと考えています。getChildView メソッドに行き詰まっています。リストから numberString を抽出する必要があります。numberString.getChild(groupPosition, childPosition) を試しましたが、「別のメソッドで定義された内部クラス内の非最終変数 groupPosition を参照できません」というエラーが発生しました。手伝ってくれてありがとう。これが私のコードです:
public class ContactActivity extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
// getExpandableListView().setOnChildClickListener(this);
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = { "General Enquiries", "Admissions Office"};
private String[][] children = {
{ "+353-91-753161" },
{ "+353-91-742305", "+353-91-742262"}
};
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(ContactActivity.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(70, 0, 0, 0);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
return textView;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
textView.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
MyExpandableListAdapter numberString = new MyExpandableListAdapter();
numberString.getChild(groupPosition, childPosition);
Uri number = Uri.parse("tel:" + numberString);
Intent dial = new Intent(Intent.ACTION_CALL, number);
startActivity(dial);
}
});
return textView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}