我似乎无法在我的expandablelist中初始化另一个textview。我已经有一个,但我想在同一个孩子中添加第二个。我为textview(ltv)创建了一个新的setter和getter,我不断得到java.lang.ArrayIndexOutOfBoundsException:length = 0;每当我尝试用我的另一个初始化它时,我的logcat中的index = 0错误。请参阅下面的代码,我缺少什么?
编辑:大小为5,每个数组我有20个元素作为示例。
MainActivity:
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;
}
}
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;
}
}
小组课程:
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;
}
}
答案 0 :(得分:2)
您的所有数组都有不同的大小,但您可以在同一个for
循环中迭代它们。您的bold[]
有5
个元素,names[]
只有10
,Images[]
只有8
;但是,您在for
循环中使用变量j
设置为迭代20
次(从0
到19
)来访问它们。< / p>
这就是您获得ArrayIndexOutOfBounds
例外的原因。它们应该具有相同的长度并与您的size
变量匹配,以便初始化size
个Child
个对象。
<小时/> 问题在于你的
Group
课程。请注意setName()
和setDetail()
如何仅将数据保存到Name
类成员。因此,您将看到两份数据副本。
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;
}
...
}
只需添加新的Detail
班级成员,然后按照我上面的操作更新相应的getDetail()
和setDetail()
方法即可解决您的问题。