Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android add Views inside a ListView

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

like image 938
Broadwell Avatar asked Jan 30 '26 13:01

Broadwell


1 Answers

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

like image 60
Olayinka Avatar answered Feb 02 '26 05:02

Olayinka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!