Reputation: 4343
I've created a listView in my MainActivity and another layout file for the row layout which looks like this:
The problem is, how do I implement it? Where do I set all the Strings that have to go in text? Where the ones that go in note?
I've looked on the internet but I could find someone who had a similar listView as mine.
I know that for Strings I can use arrays, but can I set up an array of ImageViews too?
I'd really appreciate if you could provide me with a detailed answer in which you explain me how to "put things" inside my listView
EDIT: added row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:textAllCaps="true"
android:textSize="36sp"
android:layout_marginTop="10dp"
android:fontFamily="sans-serif-condensed"
android:textStyle="bold"
android:textColor="@color/black" />
<TextView
android:id="@+id/note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text"
android:text="note note note"
android:layout_marginTop="2dp"
android:fontFamily="sans_serif_medium"
android:textSize="24sp"/>
<ImageView
android:layout_width="200dp"
android:layout_height="140dp"
android:scaleType="fitCenter"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
Upvotes: 1
Views: 311
Reputation: 5316
You should create
DataModel.java
, you can give any name of your wish ) representing your data : String text, note, image url;
along with getter and setter methods. ArrayAdapter
. Override the getView()
method. Pass the ArrayList< DataModel >
to the custom array adapter created. Create a constructor and call super to call the constructor of ArrayAdapter ": ArrayAdapter(Context context, int resource, List<T> objects)
. In the getView method inflate the row layout and set the data [ match the index from the Arraylist with the position parameter of the getView method ]
In the context of your question, I recommend you to study more about view holder pattern
and RecyclerView
, which will help you understand how things work and how they should be done efficiently.
Upvotes: 3
Reputation: 145
What you want to do is create a custom class and have it extend ArrayAdapter or BaseAdapter. In it's constructor you can pass it a String array for the text and images. In the getView method, inflate your custom row:
LayoutInflater layoutInflater = LayoutInflater.from(context);
View yourView = layoutInflater.inflate(R.layout.added_row, parent, false);
Then, you can set the values of the row using the arrays you passed to set values:
TextView text = (TextView) yourView.findViewById(R.id.text);
text.setText(text[position]);
Upvotes: 0