Hello. Stumped when creating an adapter for ListView . If you start from the beginning, that is, ArrayList , which contains a list of Week objects. Each Week object, in turn, contains a list of Day objects with 2 fields: a header and a list of Subject objects. Well, each Subject , respectively, also has a list of fields. The output of the data in the ListView should be the following: the data is output from the particular Week object, so that the Day object header first goes, and after it the data from each Subject this Day . At the same time, the data from Subject and header have different Layout . It is very difficult to describe, therefore, made a primitive sketch.

Description

Can someone suggest about this? Thank you in advance for your response.

  • Look, for example, here . - post_zeew
  • @post_zeew I've seen this, but the problem is that my header is a normal String, and the Subject is an object with 7 fields. And in this example, the data is homogeneous. - ahgpoug
  • A little later, add the answer. - post_zeew
  • @post_zeew thanks, I will wait - ahgpoug
  • already answered the question using ListView, it seems, but slightly different from the thumbnail. To get a look like on the sketch you can use CardView and RecyclerView - ZigZag

1 answer 1

Create layout files:

activity_main.xml :

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout> 

item_header.xml :

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#64B5F6"> <TextView android:id="@+id/item_header_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:padding="16dp"/> </RelativeLayout> 

item_content.xml :

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#E3F2FD"> <TextView android:id="@+id/item_content_subject_first_field_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="4dp"/> <TextView android:id="@+id/item_content_subject_second_field_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="4dp"/> <TextView android:id="@+id/item_content_subject_third_field_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="4dp"/> </LinearLayout> 

Create data model classes:

Subject :

 public class Subject { private String mFirstField; private String mSecondField; private String mThirdField; public Subject(String firstField, String secondField, String thirdField) { mFirstField = firstField; mSecondField = secondField; mThirdField = thirdField; } public String getFirstField() { return mFirstField; } public void setFirstField(String firstField) { mFirstField = firstField; } public String getSecondField() { return mSecondField; } public void setSecondField(String secondField) { mSecondField = secondField; } public String getThirdField() { return mThirdField; } public void setThirdField(String thirdField) { mThirdField = thirdField; } } 

Day :

 public class Day { private String mHeader; private ArrayList<Subject> mSubjects; public Day(String header, ArrayList<Subject> subjects) { mHeader = header; mSubjects = new ArrayList<>(subjects); } public String getHeader() { return mHeader; } public void setHeader(String header) { mHeader = header; } public ArrayList<Subject> getSubjects() { return mSubjects; } public void setSubjects(ArrayList<Subject> subjects) { mSubjects = new ArrayList<>(subjects); } } 

Week :

 public class Week { private ArrayList<Day> mDays; public Week(ArrayList<Day> days) { mDays = new ArrayList<>(days); } public ArrayList<Day> getDays() { return mDays; } public void setDays(ArrayList<Day> days) { mDays = new ArrayList<>(days); } } 

Create an adapter for ListView :

 public class CustomAdapter extends BaseAdapter { private static final int TYPE_CONTENT = 0; private static final int TYPE_HEADER = 1; private ArrayList<Day> mData; private LayoutInflater mInflater; public CustomAdapter(Context context, ArrayList<Day> data) { mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mData = new ArrayList<>(data); } @Override public int getItemViewType(int position) { return position % 4 == 0 ? TYPE_HEADER : TYPE_CONTENT; } @Override public int getViewTypeCount() { return 2; } @Override public int getCount() { return mData.size()*4; } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { int viewType = getItemViewType(position); View v = convertView; switch (viewType) { case TYPE_CONTENT: ContentViewHolder contentViewHolder; if (v == null) { v = mInflater.inflate(R.layout.item_content, null); contentViewHolder = new ContentViewHolder(); contentViewHolder.itemContentFirstSubjectTextView = (TextView) v.findViewById(R.id.item_content_subject_first_field_text_view); contentViewHolder.itemContentSecondSubjectTextView = (TextView) v.findViewById(R.id.item_content_subject_second_field_text_view); contentViewHolder.itemContentThirdSubjectTextView = (TextView) v.findViewById(R.id.item_content_subject_third_field_text_view); v.setTag(contentViewHolder); } else { contentViewHolder = (ContentViewHolder) v.getTag(); } contentViewHolder.itemContentFirstSubjectTextView.setText(mData.get(position / 4).getSubjects().get(position % 4).getFirstField()); contentViewHolder.itemContentSecondSubjectTextView.setText(mData.get(position / 4).getSubjects().get(position % 4).getSecondField()); contentViewHolder.itemContentThirdSubjectTextView.setText(mData.get(position / 4).getSubjects().get(position % 4).getThirdField()); break; case TYPE_HEADER: HeaderViewHolder headerViewHolder; if (v == null) { v = mInflater.inflate(R.layout.item_header, null); headerViewHolder = new HeaderViewHolder(); headerViewHolder.itemHeaderTextView = (TextView) v.findViewById(R.id.item_header_text_view); v.setTag(headerViewHolder); } else { headerViewHolder = (HeaderViewHolder) v.getTag(); } headerViewHolder.itemHeaderTextView.setText(mData.get(position / 4).getHeader()); break; } return v; } public static class HeaderViewHolder { public TextView itemHeaderTextView; } public static class ContentViewHolder { public TextView itemContentFirstSubjectTextView; public TextView itemContentSecondSubjectTextView; public TextView itemContentThirdSubjectTextView; } } 

Create the MainActivity :

 public class MainActivity extends AppCompatActivity { private ArrayList<Week> mWeeks; private CustomAdapter mCustomAdapter; private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.list_view); ArrayList<Day> days = new ArrayList<>(); for(int i=0; i<5; i++) { ArrayList<Subject> subjects = new ArrayList<>(); for(int j=0; j<4; j++) { subjects.add(new Subject("Header #" + i + ", subject #" + j + ", field #0", "Header #" + i + ", subject #" + j + ", field #1", "Header #" + i + ", subject #" + j + ", field #2")); } days.add(new Day("Header #" + i, subjects)); } mWeeks = new ArrayList<>(); mWeeks.add(new Week(days)); mCustomAdapter = new CustomAdapter(this, mWeeks.get(0).getDays()); mListView.setAdapter(mCustomAdapter); mCustomAdapter.notifyDataSetChanged(); } } 

As a result, we get this:

enter image description here

  • And if the header should not be every 4 elements, but depending on the length of the Subject list of each Day? Then position / 4 will not work. - ahgpoug
  • @ahgpoug, and the size of mSubjects for all Day same? - post_zeew
  • It does not matter, everything is done. Thank you for the answer. - ahgpoug