Android Custom Row Layout for ListView
When dealing with a ListView, there may be a need to customize the layout of its rows. In this case, the goal is to design rows consisting of two components: a static "HEADER" and a "Text" that changes dynamically.
Solution
To achieve this, a custom row layout is defined in the file row.xml. This layout includes the two required elements:
<code class="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="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="HEADER" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout></code>
The main activity layout (main.xml) is updated to include the ListView, and the custom adapter (yourAdapter) is implemented:
<code class="xml"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout></code>
<code class="java">public class yourAdapter extends BaseAdapter { Context context; String[] data; private static LayoutInflater inflater = null; // ... (Constructor and methods as per the provided answer) }</code>
Finally, in the Java activity class (StackActivity), the adapter is set to the ListView:
<code class="java">public class StackActivity extends Activity { ListView listview; // ... (Activity methods as per the provided answer) listview.setAdapter(new yourAdapter(this, new String[] { "data1", "data2" })); }</code>
The result is a ListView with custom rows displaying the static HEADER and dynamic Text content.
The above is the detailed content of How to Create a Custom Row Layout with a Static Header and Dynamic Text in Android ListView?. For more information, please follow other related articles on the PHP Chinese website!