I have a cardview with 2 textviews inside. The first textview displays the name of a contact and the second displays the phone number. I want to use an ArrayList to populate the recyclerview.
I'm still new to Android programming so I don't know how to implement RecyclerView or make an adapter that will alternate between name and number filling in the textview. How can I achieve this?
My CardView (card_row.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:clickable="true"
android:orientation="horizontal"
card_view:cardCornerRadius="0dp"
card_view:cardUseCompatPadding="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="38dp"
android:layout_margin="5dp"
android:gravity="left"
android:textColor="#android:color/black"
android:textSize="24sp" />
<TextView
android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="37dp"
android:layout_margin="5dp"
android:layout_below="#id/text1"
android:textColor="#android:color/darker_gray"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
EDIT:
I made 2 arraylists, one for names and the other for numbers. Now I how do I populate the recyclerview?
OK First make a inner class. This will be your adapter change all the id:
class Adapter extends RecyclerView.Adapter<LibraryAdapter.MyViewHolder>{
LayoutInflater inflater;
public LibraryAdapter(Context context){
inflater = LayoutInflater.from(context);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view =inflater.inflate(R.layout.main_exerciselibrary_fragment_singlerow,parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Exercises exercises = new Exercises(getActivity());
ArrayList<String> arrayList1 = new ArrayList<String>();
ArrayList<String> arrayList2 = new ArrayList<String>();
holder.Name.setText(arrayList1.get(position));
holder.PhoneNumber.setText(arrayList2.get(position));
}
#Override
public int getItemCount() {
return array.lenght;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView Name, PhoneNumber;
CardView card;
public MyViewHolder(View itemView) {
super(itemView);
PhoneNumber= (TextView) itemView.findViewById(R.id.imageViewPicLibrary);//change Here
Name = (TextView) itemView.findViewById(R.id.textViewExerciseLibraryExerciseName);//change Here
card= (CardView) itemView.findViewById(R.id.card_view_ex_lib);//change Here
card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//OnClick
}
});
}
}
}
I think this is all.
Related
I have a layout for a listview item.
I added an ImageButton to the layout, as follows:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tvTaskName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="20dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/tvTaskDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description"
android:textSize="14sp"
android:textColor="#0E2DF5"
android:layout_marginTop="40dp"
/>
<TextView
android:id="#+id/tvTaskStartTime"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Start time"
android:textSize="14sp"
android:textColor="#44FF00"
android:layout_marginHorizontal="100dp"
android:layout_marginTop="10dp"
/>
<TextView
android:id="#+id/tvTaskDeadLine"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Dead line"
android:textSize="14sp"
android:textColor="#44FF00"
android:layout_marginHorizontal="100dp"
android:layout_marginTop="40dp"
/>
<ImageButton
android:id="#+id/btnTaskDone"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginStart="345dp"
android:background="#drawable/done_button_state_selector"
/>
</FrameLayout>
The problem - when I add the ImageButton - the entire ListView becomes unclickable. I can only click the button.
I need both the ListView and the button to be clickable, because im using them for different purposes.
I tried putting the ImageButton in one FrameLayout, everything else in another FrameLayout, and both of the FrameLayouts in on big FrameLayout. Still nothing
I would be happy to hear any suggestions. Thanks!
I would use a recyclerView instead it allows for more flexibility, you can have your recyclerView in your xml code like so
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar" />
Then you set it up like so with java code
RecyclerView recycler;
CustomAdapter adapter;
List<CustomModel> models = new ArrayList<>();
private DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kids_search);
recycler= findViewById(R.id.recycler);
adapter = new CustomAdapter (models);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recycler.setLayoutManager(layoutManager);
recycler.setAdapter(searchAdapter);
}
So you create a model class to hold variables for each list item to show then you populate the models ArrayList with those models.
Here is how the the most important customAdapter class should look like
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
List<CustomModel> customModels;
Context context;
public CustomAdapter(List<ChildModel> childModels) {
this.childModels = childModels;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_child, parent, false);
context = parent.getContext();
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final CustomModel customModel = customModels.get(position);
holder.tvTaskName.setText(customModel.getTaskName());
holder.tvTaskDescription.setText(customModel.getDescreption());
holder.tvTaskStartTime.setText(customModel.getStartTime());
holder.tvTaskDeadLine.setText(customModel.getDeadLine());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Here the list item is clicked
}
});
holder.btnTaskDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Here the imageButton is clicked
}
});
}
#Override
public int getItemCount() {
return childModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvTaskName;
TextView tvTaskDescription;
TextView tvTaskStartTime;
TextView tvTaskDeadLine;
ImageButton btnTaskDone;
public ViewHolder(#NonNull View itemView) {
super(itemView);
tvTaskName = itemView.findViewById(R.id.tvTaskName);
tvTaskDescription = itemView.findViewById(R.id.tvTaskDescription);
tvTaskStartTime = itemView.findViewById(R.id.tvTaskStartTime);
tvTaskDeadLine = itemView.findViewById(R.id.tvTaskDescription);
btnTaskDone = itemView.findViewById(R.id.btnTaskDone);
}
}
}
Hope this helps
i want to reuse an inflated view just the same way a listView adapter does with convertView. Ive been lurking on the source code of adapters with no luck.
I need to add dinamically views depending on data to a recyclerItemView, now im inflating as much times as i need the view but having in mind its inside a recyclerView, this operation could become absurdly costly depending on the amount of data and scrolling behaviour of the user.
Other solution to my problem would be to create a listview inside the recyclerItem, so the listview wouldnt be scrollable, expands to maximum height depending on data added dynamically(making the listview container to expand to show all its data) and make the recyclerItem expand depending on its height.
ACTIVITY
public class Main1Activity extends AppCompatActivity {
RecyclerView recyclerView;
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
setUpRecycler();
}
private void setUpRecycler(){
Sample sample1 = new Sample("List of Items1");
Sample sample2 = new Sample("List of Items2");
Sample sample3 = new Sample("List of Items3");
List<Sample> sampleList = new ArrayList<>();
sampleList.add(sample1);
sampleList.add(sample2);
sampleList.add(sample3);
adapter = new CustomAdapter(this,sampleList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
MODEL
public class Sample {
String name;
public Sample(){}
public Sample(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
XML ITEMS
**item_list**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
**item_smaple**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_margin="10dp"
android:text="List of Items"
android:textSize="20sp"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:orientation="vertical"/>
</LinearLayout>
ADAPTER
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private List<Sample> mySamples;
private Context mContext;
public CustomAdapter(Context context, List<Sample> mySamples) {
this.mContext = context;
this.mySamples = mySamples;
}
private Context getContext() {
return mContext;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
}
}
#Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.item_sample, parent, false);
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
#Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {
Sample sample = mySamples.get(position);
holder.name.setText(sample.getName());
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.item_list, null, false);
for (int i = 0; i < 20; i++){
TextView textView = (TextView) v.findViewById(R.id.textView);
textView.setText("hello"+i);
holder.linearLayout.addView(v);
}
}
#Override
public int getItemCount() {
return mySamples.size();
}
}
Right now i´m inflating inside the loop. How could i modify view parameters to make it reusable to the framework?
My app crashes on the addView()
-->java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
The issue that you are referring to is the one which was addressed by creating RecyclerView which is an upgrade to ListView. What RecyclerView does is that it will create/inflate say 5-10 list item and when the user starts scrolling new listItem view(s) are not inflated infact just the data is updated inside the listItem.
This is a great explainer video of how recycler view actually works with a bit of code as well. https://www.youtube.com/watch?v=Wq2o4EbM74k
I hope this clears your doubt regarding the inflation issue.
My app crashes on the addView() -->java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
try:
#Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {
Sample sample = mySamples.get(position);
holder.name.setText(sample.getName());
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context
.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 20; i++) {
View v = inflater.inflate(R.layout.item_list, holder.linearLayout, false);
TextView textView = (TextView) v.findViewById(R.id.textView);
textView.setText("hello" + i);
holder.linearLayout.addView(v);
}
}
itemSample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="10dp"
android:text="List of Items"
android:textSize="20sp"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="30dp"
android:orientation="vertical"
android:isScrollContainer="true"
/>
</LinearLayout>
itemList.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="40dp"
android:isScrollContainer="true"
android:id="#+id/txt_cont"
>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
You should try answer #
I am having problems displaying all the items in my recyclerview. My code only shows one item (Home. I have 3 items: Home, Browse Photos and My Travels)
fragment_navigation_drawer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_navigation_drawer"
android:background="#EEE"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.traveldiaries.ztir.traveldiaries.NavigationDrawerFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#16a085"
android:id="#+id/linearLayout">
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<MenuList> data = Collections.emptyList();
public RecyclerAdapter(Context context, List<MenuList> data){
inflater= LayoutInflater.from(context);
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.itemlist,parent,false);
MyViewHolder holder= new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MenuList current=data.get(position);
holder.title.setText(current.title);
holder.image.setImageResource(current.icon);
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.item_list);
image = (ImageView) itemView.findViewById(R.id.icons);
}
}
}
NavigationDrawerFragment.java (posted code only related to recyclerview)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new RecyclerAdapter(getActivity(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<MenuList> getData(){
List<MenuList> data = new ArrayList<>();
String[] titles={"Home","My Travels","Browse Photos"};
int[] icons ={R.drawable.home,R.drawable.sunglasses,R.drawable.tooltip_image};
for(int i=0;i<3;i++){
MenuList current = new MenuList();
current.title=titles[i];
current.icon=icons[i];
Log.d("Data",titles[i]);
Log.d("Data",""+icons[i]);
data.add(current);
}
return data;
}
itemlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:padding="8dp"
android:id="#+id/icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy Text"
android:layout_gravity="center_vertical"
android:padding="8dp"
/>
</LinearLayout>
What I was able to do is to check all the passed data (all seems to be fine). Also tried to change width to wrap content (fragment_navigation_drawer.xml) in both my relative and linear layout but I just lose my background color instead and still only one item appear.
Change height of LinearLayout to wrap_content in itemlist.xml
android:layout_height="wrap_content"
Just change the height of your itemlist from "match_parent" to "wrap_content".
I'm using CarViews with RecyclerView and it looks fine when loaded but once when the list is scrolled the gap between the CardViews increases and there shows only one card in the view at a time.
Here is my CardView.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/conversationCard"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="#+id/sender"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/abstractConvo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
and here is how I'm using that view
public class ConversationsListAdapter extends RecyclerView.Adapter<ConversationsListAdapter.ConversationsListViewHolder> {
List<Conversation> conversationList;
public ConversationsListAdapter(List<Conversation> conversationList) {
this.conversationList = conversationList;
}
#Override
public int getItemCount() {
return conversationList.size();
}
#Override
public ConversationsListViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.conversation_card, viewGroup, false);
ConversationsListViewHolder listViewHolder = new ConversationsListViewHolder(view);
return listViewHolder;
}
#Override
public void onBindViewHolder(ConversationsListViewHolder holder, int position) {
holder.sender.setText(conversationList.get(position).getSenderPhNo());
holder.abstractConvo.setText(conversationList.get(position).getAbstractConvo());
}
public static class ConversationsListViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView abstractConvo, sender;
public ConversationsListViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.conversationCard);
sender = (TextView) itemView.findViewById(R.id.sender);
abstractConvo = (TextView) itemView.findViewById(R.id.abstractConvo);
}
}
}
Here is the screenshot before the list is scrolled
and after scrolling
Thanks in advance.
the root layout use wrap_content
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
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
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