2013-03-28 105 views
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; 
     } 

    } 
} 

回答

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); 
    } 
}); 

是,增加final到groupPosition和childPosition在onclick就能解决问题

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

这是因为,在Java中,您不能访问类型为View.OnClickListener

的匿名内部类中的非最终变量
+0

仍然有相同的错误 – ciastkoo 2013-03-28 10:45:46