3

how can I add Views inside a LinearLayout in a ListView? I want to display some additional information inside the ListView and I want to add them in a for loop going through the items to add. Here's the code I currently have:

Activity:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:scrollbars="vertical"
            android:layout_marginTop="0dp" />

</LinearLayout>

list_recycler:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="6dp"
        android:layout_marginTop="6dp"
        android:layout_marginBottom="6dp">

        <TextView android:id="@+id/list_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/textColor"
            android:textSize="16sp"
            android:layout_alignParentLeft="true"/>

        <TextView android:id="@+id/list_title_summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/textColor"
            android:textSize="16sp"
            android:layout_alignParentLeft="true"/>


        <!-- VIEWS SHOULD BE ADDED HERE -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

Adapter:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.RecyclerViewHolder> {

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

        TextView personName;
        TextView personAge;

        RecyclerViewHolder(View itemView) {
            super(itemView);
            personName = (TextView)itemView.findViewById(R.id.person_name);
            personAge = (TextView)itemView.findViewById(R.id.person_age);
        }
    }

    List<Items> list;

    public RVAdapter(List<Items> list){
        this.list = list;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_recycler, viewGroup, false);
        return new RecyclerViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int i) {
        recyclerViewHolder.personName.setText(persons.get(i).name);
        recyclerViewHolder.personAge.setText(persons.get(i).age);
    }

    @Override
    public int getItemCount() {
        return persons.size();
    }
}

Items:

public class Items {
    String name;
    String age;

    public Items(String name, String age) {
        this.name = name;
        this.age = age;
    }
}

How can I add views inside the LinearLayout in the ListView? Each LinearLayout in the different ListView Items should contain different views, so they can't all be the same.

How my data will look like:

ListView
    Entry1
        LinearLayout
            SubEntry1
            SubEntry2
            SubEntry3
    Entry2
        LinearLayout
            SubEntry1
            SubEntry2
            SubEntry3
    Entry3
        LinearLayout
            SubEntry1
            SubEntry2
            SubEntry3

So Entry1..Entry3 are ListViews, all the subentries should be TextViews added by code into the LinearLayout inside its corresponding ListView entry

Broadwell
  • 1,343
  • 2
  • 19
  • 33
  • See the link bellow http://stackoverflow.com/questions/4540754/dynamically-add-elements-to-a-listview-android – Anis LOUNIS aka AnixPasBesoin May 17 '15 at 19:08
  • I don't want to add items to the ListViews, I want to add Views (like a TextView) inside the LinearLayout inside the ListView. I know how to add items to the ListView – Broadwell May 17 '15 at 19:15
  • 1
    If you want to 'add' a TextView inside your ListView custom row why not have it there in the layout already with the attribute android:visibility="gone" . Then change its attribute to 'VIEW.VISIBLE' on selected rows you want it visible inside your custom adapter. – Mark May 17 '15 at 19:37
  • Because I don't know how many TextViews there are going to be because the data gets parsed from the web and changes daily – Broadwell May 17 '15 at 19:41
  • Do you have an idea of a maximum number or no idea at all? If you do then just have the maximum amount in there with all set to "gone" and set the required amount to view.visible on each row? – Mark May 17 '15 at 19:46
  • I do not have any idea how many there are going to be, that's the reason I want to create them programmatically so I can just use a loop to loop through the count – Broadwell May 17 '15 at 19:49
  • Check [this question](http://stackoverflow.com/questions/28429906/how-to-add-multiple-custom-linearlayouts-programmatically-to-listview-item), you can add any amount of sub-items to your listview items. – Jonas Czech May 17 '15 at 19:58
  • I don't understand where to add that code. Also I am using RecyclerView now based on the suggestion by @Olayinka – Broadwell May 17 '15 at 20:01

1 Answers1

3

Give the layout an id, say @+id/item_content, then in your getViewdo:

itemContent = (LinearLayout)convertView.findViewById(R.id.item_content);
itemContent.removeAllViews();
//then you can add your views to the layouts depending on each item here using 
itemContent.addView(yourView);

You can't simply loop over the ListView items and add content to it. That is what we have the adapter for. Update the data and notify the adapter tht the data has changed.

I'll advice you use RecyclerView in this context.

EDIT

What you added is not a data structure but your layout structure. Anyways, you should have this.

public class RowItem {
    private final String title;
    private final String desc;
    private ArrayList<String> contents;
}

and then you'll do this;

itemContent = (LinearLayout)convertView.findViewById(R.id.item_content);
itemContent.removeAllViews();
//then you can add your views to the layouts depending on each item here using 
for(String item : rowItem.getContents()){
    TextView textView = new TextView(parent.getContext());
    textView.setText(item);
    itemContent.addView(textView);
}

Read RecyclerView and ExpandableListView

Olayinka
  • 2,813
  • 2
  • 25
  • 43
  • Could you please post a little bit more code, how I would implement it? – Broadwell May 17 '15 at 19:35
  • @Broadwell Tell us what your data look like. – Olayinka May 17 '15 at 19:35
  • "I'll advice you use RecyclerView in this context." there's no reasoning given for this advice; your answer applies just as equally to a ListView: i.e., **when the data is fetched, update the adapter. The adapter should build each item view and customise it according to the item at the requested position** – ataulm May 17 '15 at 19:55
  • I updated the question and am now using RecyclerView – Broadwell May 17 '15 at 19:57
  • Where exactly should I put this code? In the adapter? or where I am loading the data into the adapter? – Broadwell May 17 '15 at 19:58
  • @ataulm I expect the OP to read up RecyclerView. it was an advice, I didn't insist on it. The code snippet I gave is based on ListView. – Olayinka May 17 '15 at 19:58