2013-03-28 2 views
0

헤더와 숫자가있는 확장 가능한 목록이 있습니다. 목록의 번호를 클릭 할 때마다 다이얼러를 사용하여 작업을 완료하고 싶습니다. Im은 getChildView 메소드를 고수했습니다. 목록에서 numberString을 추출해야합니다. 나는 numberString.getChild (groupPosition, childPosition)를 시도했지만 "다른 메서드에서 정의 된 내부 클래스 내부의 final 변수가 아닌 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 View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, 
       View convertView, ViewGroup parent) { 
를 해결합니다 온 클릭에 groupPosition 및 childPosition에 final를 추가하는 데이터 배열 직접 (이 경우 어린이)

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

예 : 여기 내 코드입니다

Java에서는 익명 내부 클래스 View.OnClickListener

내부의 최종 변수에 액세스 할 수 없으므로
+0

에도 여전히 같은 오류가 있습니다. – ciastkoo