Я пытаюсь сделать расширяемый список, но ребенок должен иметь 5 элементов только в одной строке, я пытаюсь сделать адаптер, но я не знаю, как это сделать. Это мой adapter's код:Расширяемый список
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ChildModel childText = (ChildModel) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
TextView txtListChilddos = (TextView) convertView
.findViewById(R.id.textView1);
TextView txtListChildtres = (TextView) convertView
.findViewById(R.id.textView2);
TextView txtListChildcuatro = (TextView) convertView
.findViewById(R.id.textView3);
TextView txtListChildcinco = (TextView) convertView
.findViewById(R.id.textView4);
txtListChild.setText(childText.getDate());
txtListChilddos.setText(childText.getSong());
txtListChildtres.setText(childText.getDuration());
txtListChildcuatro.setText(childText.getArtist());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
В моей деятельности, если я делаю это создает одну строку для каждого элемента, и я хочу 5 элементов в одной строке списка:
List<String> mychildlist;
mychildlist.add(date);
mychildlist.add(name);
mychildlist.add(song);
mychildlist.add(duration);
mychildlist.add(artist);
Мне нравится список, как это:
+rock
--------------
George Michael
song´s title
5 min
lp
--------------
U2
song´s title
4 min
cd
--------------
Спасибо вам так много
и как я могу создать новый объект? как это? michild = new ChildModel(); michild.setDate (дата); michild.setName (имя); michild.setSong (песня); michild.setDuration (продолжительность); michild.setArtist (artist); – user1096087
да. и вы также можете добавлять параметры к своему конструктору вместо вызова методов setter. –
У меня есть ClassCastException в строке final ChildModel child = (ChildModel) getChild (groupPosition, childPosition) ;, вы можете мне помочь? – user1096087