Java 在ExpandableListView中有多个TextView时遇到问题
我似乎在初始化expandablelist中的另一个textview时遇到问题。我已经有了一个,但我正试图在同一个孩子身上添加第二个。我为textview(ltv)创建了一个新的setter和getter,并不断获取java.lang.ArrayIndexOutOfBoundsException:length=0;每当我尝试用另一个日志初始化它时,我的日志猫中的index=0错误。请看下面我的代码,我遗漏了什么 编辑:大小是5,例如,我每个数组有20个元素 主要活动:Java 在ExpandableListView中有多个TextView时遇到问题,java,android,arraylist,expandablelistview,Java,Android,Arraylist,Expandablelistview,我似乎在初始化expandablelist中的另一个textview时遇到问题。我已经有了一个,但我正试图在同一个孩子身上添加第二个。我为textview(ltv)创建了一个新的setter和getter,并不断获取java.lang.ArrayIndexOutOfBoundsException:length=0;每当我尝试用另一个日志初始化它时,我的日志猫中的index=0错误。请看下面我的代码,我遗漏了什么 编辑:大小是5,例如,我每个数组有20个元素 主要活动: public class
public class Brandspage extends Activity {
private ExpandListAdapter ExpAdapter;
private ExpandableListView ExpandList;
private ArrayList<Group> ExpListItems;
private TextView brandtv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brandspage);
Button b1 = (Button) findViewById(R.id.button4);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Brandspage.this, MainActivity.class);
startActivity(i);
}
});
ExpandList = (ExpandableListView) findViewById(R.id.exp_list);
ExpListItems = SetStandardGroups();
ExpAdapter = new ExpandListAdapter(Brandspage.this, ExpListItems);
ExpandList.setAdapter(ExpAdapter);
}
public ArrayList<Group> SetStandardGroups() {
String group_names[] = {"A-G", "H-N", "O-U", "V-Z"
};
String bold[] = {"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15","16",
"17","18","19","20"
//This is the array I'm trying to initialize with the ltv textview
};
String names[] = { "Brazil", "Mexico", "Croatia", "Cameroon",
"Netherlands", "chile", "Spain", "Australia", "Colombia",
"Greece", "Greece", "chile", "chile", "chile", "chile","Colombia","Colombia","Colombia","Colombia","Colombia"
};
int Images[] = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
};
ArrayList<Group> list = new ArrayList<Group>();
ArrayList<Child> ch_list;
int size = 5;
int j = 0;
int k = 0;
for (String group_name : group_names) {
Group gru = new Group();
gru.setName(group_name);
ch_list = new ArrayList<Child>();
for ( j=0; j < size; j++) {
Child ch = new Child();
//this is not working?
ch.setDetail(bold[j]);
ch.setName(names[j]);
ch.setImage(Images[j]);
ch_list.add(ch);
}
gru.setItems(ch_list);
list.add(gru);
size = size + 5;
}
return list;
}
}
public class Brandspage扩展活动{
专用ExpandListAdapter;
私有可扩展列表视图可扩展列表;
私有ArrayList解释项;
私人文本视图品牌电视;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brandspage);
按钮b1=(按钮)findViewById(R.id.button4);
b1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意图i=新意图(Brandspage.this,MainActivity.class);
星触觉(i);
}
});
ExpandList=(ExpandableListView)findViewById(R.id.exp\u list);
ExpListItems=SetStandardGroups();
ExpAdapter=新ExpandListAdapter(Brandspage.this,ExpListItems);
ExpandList.setAdapter(ExpAdapter);
}
公共阵列列表集合标准组(){
字符串组_名称[]={“A-G”、“H-N”、“O-U”、“V-Z”
};
字符串粗体[]={“1”、“2”、“3”、“4”、“5”,
"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15","16",
"17","18","19","20"
//这是我试图用ltv文本视图初始化的数组
};
字符串名称[]={“巴西”、“墨西哥”、“克罗地亚”、“喀麦隆”,
“荷兰”、“智利”、“西班牙”、“澳大利亚”、“哥伦比亚”,
“希腊”、“希腊”、“智利”、“智利”、“智利”、“智利”、“哥伦比亚”、“哥伦比亚”、“哥伦比亚”
};
int Images[]={R.drawable.ic_启动器,R.drawable.ic_启动器,R.drawable.ic_启动器,R.drawable.ic_启动器,
R.drawable.ic_发射器,R.drawable.ic_发射器,R.drawable.ic_发射器,R.drawable.ic_发射器,R.drawable.ic_发射器,
R.drawable.ic_发射器,R.drawable.ic_发射器,R.drawable.ic_发射器,R.drawable.ic_发射器,R.drawable.ic_发射器,
R.drawable.ic_发射器,R.drawable.ic_发射器,R.drawable.ic_发射器,R.drawable.ic_发射器,R.drawable.ic_发射器,R.drawable.ic_发射器,
};
ArrayList=新建ArrayList();
ArrayList Chu列表;
int size=5;
int j=0;
int k=0;
for(字符串组名称:组名称){
gru组=新组();
gru.setName(组名称);
chu list=newarraylist();
对于(j=0;j
ExpandablelistAdapter:
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Group> groups;
public ExpandListAdapter(Context context, ArrayList<Group> groups) {
this.context = context;
this.groups = groups;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Child child = (Child) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.country_name);
ImageView iv = (ImageView) convertView.findViewById(R.id.flag);
TextView ltv = (TextView) convertView.findViewById(R.id.large);
tv.setText(child.getName());
iv.setImageResource(child.getImage());
//Trying to do this textview ltv
ltv.setTypeface(null, Typeface.BOLD);
ltv.setText(child.getDetail());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {return false;
}
}
公共类ExpandListAdapter扩展了BaseExpandableListAdapter{
私人语境;
私有ArrayList组;
公共ExpandListAdapter(上下文、ArrayList组){
this.context=上下文;
这个组=组;
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
ArrayList chList=groups.get(groupPosition.getItems();
返回chList.get(childPosition);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、,
布尔值isLastChild、视图转换视图、视图组父级){
Child=(Child)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.flate(R.layout.child_项,空);
}
TextView tv=(TextView)convertView.findViewById(R.id.country\u name);
ImageView iv=(ImageView)convertView.findViewById(R.id.flag);
TextView ltv=(TextView)convertView.findViewById(R.id.large);
tv.setText(child.getName());
iv.setImageResource(child.getImage());
//正在尝试执行此操作textview ltv
ltv.setTypeface(null,Typeface.BOLD);
ltv.setText(child.getDetail());
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
ArrayList chList=groups.get(groupPosition.getItems();
返回chList.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回groups.get(groupPosition);
}
@凌驾
public int getGroupCount(){
返回组。size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共视图getGroupView(int-groupPosition,布尔值isExpanded,
视图(视图、视图组父级){
组组=(组)getGroup(组位置);
if(convertView==null){
LayoutInflater inf=(LayoutInflater)上下文
.getSystemService(上下文布局\充气机\服务);
convertView=inf.inflate(R.layout.group_项,空);
}
TextView tv=(TextView)convertView.findViewById(R.id.group\u name);
tv.setText(group.getName());
import java.util.ArrayList;
public class Group {
private String Name;
private ArrayList<Child> Items;
public String getName() {
return Name;
}
public String getDetail() {
return Name;
}
public void setDetail(String name) {
this.Name = name;
}
public void setName(String name) {
this.Name = name;
}
public ArrayList<Child> getItems() {
return Items;
}
public void setItems(ArrayList<Child> Items) {
this.Items = Items;
}
}
public class Group {
private String Name;
private String Detail; // Add a new Detail member
private ArrayList<Child> Items;
public String getName() {
return Name;
}
public String getDetail() {
return Detail; // change getter
}
public void setDetail(String Detail) {
this.Detail = Detail; // change setter
}
public void setName(String name) {
this.Name = name;
}
...
}