2013-03-28 47 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