Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple custom linearLayouts programmatically to listView item

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:enter image description here

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);
   }
like image 545
Rishka Avatar asked Feb 10 '15 10:02

Rishka


1 Answers

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.

like image 120
JonasCz Avatar answered Sep 20 '22 02:09

JonasCz