I\'ve got a quite irritating problem. My task is to add multiple LinearLayouts with custom XML template (And I don\'t know how many of Layouts there could be) with 2 TextVie
In the getView() method of your adapter, you inflate the main XML layout for the list item. Then you should loop through your data (Array, Cursor, whatever) and for each extra LinearLayout you want, create it, and add the TextViews, set their text, and add it to the main layout you inflated from XML. See this answer.
If you are re-using the view from the convertView parameter (which you should be doing), you must also ensure that your main layout is empty before using it, which you can do withmyMainListItemLayout.removeAllViews()
A different way might be to put a listView inside your listview. (probably not such a good idea.)
Edit: Try changing your code as follows:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
convertView = inflater.inflate(R.layout.parsed_csv_list_view_main_item, parent, false);
} else {
((LinearLayout) convertView).removeAllViews();
}
RealmResults<ParsedCSV> titles = parentFragment.getParsedCSVtitles();
String[] parsedTitles = titles.get(0).getValues().split(";");
String[] parsedValues = items.get(position).getValues().split(";");
for (int i = 0; i < parsedValues.length; i++) {
View holder = inflater.inflate(R.layout.parsed_csv_list_view_subitem, parent, false);
TextView textViewTitles = (TextView) holder.findViewById(R.id.parsed_csv_list_view_subitem_text_title);
TextView textViewValues = (TextView) holder.findViewById(R.id.parsed_csv_list_view_subitem_text_value);
textViewTitles.setText(parsedTitles[i]);
textViewValues.setText(parsedValues[i]);
((LinearLayout) convertView).addView(holder);
}
return ((View)convertView);
}
It's ugly, and probably slightly slow, but it works.