2017-11-29 2 views
1

두 자녀가 포함 된 ExpandableListView에 하나의 그룹이 있습니다. 클릭 한 자녀의 이름으로 그룹 이름을 설정하고 싶습니다.onChildClickListener, expandableListView에서 groupName을 변경하는 중

getGroupView() 방법으로 생성되는 TextView에 대한 액세스 권한을 얻으려면 어떻게해야합니까? 어댑터에 데이터를 입력하지 마십시오 더 나은 방식으로

CustomAdapter customAdapter = new CustomAdapter(this); 
    expandableListView.setAdapter(customAdapter); 

    expandableListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

     } 
    }); 
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, int group, int position, long id) { 

      String tekst = (String) parent.getExpandableListAdapter().getChild(group,position); 
      parent.collapseGroup(group); 

      Toast.makeText(getApplicationContext(), tekst ,Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
}); 

CustomAdapter.class:

private Context context; 
public CustomAdapter(Context context) 
{ 
    this.context = context; 
} 

private String[] grupa = {"Płeć"}; 
private String[][] dzieci = {{"Kobieta","Mężczyzna"}}; 

//region gettersFolded 
@Override 
public int getGroupCount() { 
    return grupa.length; 
} 

@Override 
public int getChildrenCount(int position) { 
    return dzieci[position].length; 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return grupa[groupPosition]; 
} 

@Override 
public Object getChild(int group, int position) { 
    return dzieci[group][position]; 
} 

@Override 
public long getGroupId(int group) { 
    return group; 
} 

@Override 
public long getChildId(int group, int position) { 
    return position; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) { 
    TextView textView = new TextView(context); 
    textView.setText(grupa[groupPosition]); 
    textView.setPadding(10,10,10,10); 
    textView.setBackgroundResource(R.drawable.btm_line_shape); 
    textView.setTextSize(14); 
    return textView; 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isExpanded, View view, ViewGroup parent) { 
    TextView textView = new TextView(context); 
    textView.setText(dzieci[groupPosition][childPosition]); 
    textView.setTextSize(12); 
    textView.setPadding(10,10,10,10); 
    return textView; 
} 

@Override 
public boolean isChildSelectable(int i, int i1) { 
    return true; 
} 
//endregion Region 

답변

1

1에 기록 될 수 있다면 나는 기꺼이 코드에 대한 몇 가지 팁을들을 그런데

. 확장 가능한 ListView 또는 string.xml의 Activity에 속합니다.

2 : expandableListView를 새로 고치려면 새로 고친 데이터가있는 새 어댑터를 ListView에 설정하십시오.

귀하의 활동 :

grupa = "Płeć";        //declare as String 
dzieci = {"Kobieta","Mężczyzna"};   //declare as String[] 
customAdapter = new CustomAdapter (this, grupa, dzieci); 
expandableListView.setAdapter(customAdapter); 
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
    @Override 
    public boolean onChildClick(ExpandableListView parent, View v, int group, int position, long id) { 

     String tekst = (String) parent.getExpandableListAdapter().getChild(group,position); 
     parent.collapseGroup(group); 

     Toast.makeText(getApplicationContext(), tekst ,Toast.LENGTH_SHORT).show(); 
     grupa = dzieci[position]; 
     customAdapter = new CustomAdapter (yourApplication.this, grupa, dzieci); 
     expandableListView.setAdapter(customAdapter); 
     return false; 
    } 
}); 

어댑터 :

private Context context; 
private String grupa; 
private String[] dzieci; 
public CustomAdapter(Context context, String groupNames, String[] childNames) 
{ 
    this.context = context; 
    this.grupa = groupNames; 
    this.dzieci = childNames; 
} 

//region gettersFolded 
@Override 
public int getGroupCount() { 
    return 1; 
} 

@Override 
public int getChildrenCount(int position) { 
    return dzieci.length; 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return grupa; 
} 

@Override 
public Object getChild(int group, int position) { 
    return dzieci[position]; 
} 

@Override 
public long getGroupId(int group) { 
    return group; 
} 

@Override 
public long getChildId(int group, int position) { 
    return position; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) { 
    TextView textView = new TextView(context); 
    textView.setText(grupa); 
    textView.setPadding(10,10,10,10); 
    textView.setBackgroundResource(R.drawable.btm_line_shape); 
    textView.setTextSize(14); 
    return textView; 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isExpanded, View view, ViewGroup parent) { 
    TextView textView = new TextView(context); 
    textView.setText(dzieci[childPosition]); 
    textView.setTextSize(12); 
    textView.setPadding(10,10,10,10); 
    return textView; 
} 

@Override 
public boolean isChildSelectable(int i, int i1) { 
    return true; 
} 

당신이 대신 다차원 배열의 HashMaps을 시도해야 여러 그룹을 사용하려면

. 이 동영상의 도움 : https://www.youtube.com/watch?v=jZxZIFnJ9jE

관련 문제