0

Я пытаюсь сделать расширяемый список, но ребенок должен иметь 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 
-------------- 

Спасибо вам так много

ответ

0

скажем, у вас есть какие-то предметы, как ребенок, и каждый из них имеет 5 строк внутри, и вы хотите, чтобы показать их. первых, создать модель данных ребенка:

public class ChildModel { 
    String date; 
    String name; 
    String song; 
    String duration; 
    String artist; 
    // constructor and getter setters 
    } 

теперь вы должны реализовать адаптер:

public View getChildView(int groupPosition, final int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 

    final ChildModel child = (ChildModel) getChild(groupPosition, childPosition); 

    // init yout layout 
    ... 

    TextView txtListChildDate = (TextView) convertView 
      .findViewById(R.id.item_date_text_view); 
    TextView txtListChildName = (TextView) convertView 
      .findViewById(R.id.item_name_text_view); 
    TextView txtListChildSong = (TextView) convertView 
      .findViewById(R.id.item_song_text_view); 
    TextView txtListChildDuration = (TextView) convertView 
      .findViewById(R.id.item_duration_text_view); 
    TextView txtListChildArtist = (TextView) convertView 
      .findViewById(R.id.item_artist_text_view); 


    txtListChildDate.setText(child.getDate()); 
    txtListChildName.setText(child.getName(); 
    txtListChildSong.setText(child.getSong()); 
    txtListChildDuration.setText(child.getDuration()); 
    txtListChildArtist.setText(child.getArtist()); 

    return convertView; 
} 
+0

и как я могу создать новый объект? как это? michild = new ChildModel(); michild.setDate (дата); michild.setName (имя); michild.setSong (песня); michild.setDuration (продолжительность); michild.setArtist (artist); – user1096087

+0

да. и вы также можете добавлять параметры к своему конструктору вместо вызова методов setter. –

+0

У меня есть ClassCastException в строке final ChildModel child = (ChildModel) getChild (groupPosition, childPosition) ;, вы можете мне помочь? – user1096087

0

Создание двух пользовательских объектов:

class Group { 
String groupname; 
ArrayList<Child> childsarray; 
} 

class Child{ 
String item1; 
String item2; 
String item3; 
String item4; 
String item5; 
} 

Pass групп массив в адаптере.

Смежные вопросы