Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

android - 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);
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...