0

我有一个带有标题和数字的可扩展列表。每当单击列表中的号码时,我都想使用拨号器完成操作。我坚持使用 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;
        }

    }
}
4

1 回答 1

0

直接访问数据数组(本例中为子数组)

public void onClick(View view){
    String numberString = children[groupPosition][childPosition]
    Uri number = Uri.parse("tel:" + numberString);
    Intent dial = new Intent(Intent.ACTION_CALL, number);
    startActivity(dial);
    }
});

可以,在onClick中加入finalgroupPosition和childPosition就可以解决问题

public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {

这是因为,在 Java 中,您无法访问类型的匿名内部类中的非最终变量View.OnClickListener

于 2013-03-28T10:42:13.143 回答