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 TextViews inside (in each) into 1 ListView item.
Like this:
Is it possible to do this in ArrayAdapter? Any help would be much appreciated!
Ok, i've managed to did something with help from Jonas Cz.
And this is what I've got.
@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);
}
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);
}
I had to cast my convertView to LinearLayout because just converView didn't had addView method. Ok, it's a shit but it works...with some problem.
Now, while scrolling, it seems like the amount of items in list increases for some reason. Can someone explain me why is this happening and how to fix it?
FULLY WORKING SOLUTION FROM JonasCz:
@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);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…