Home > Java > javaTutorial > body text

How to Create a Custom Row Layout with a Static Header and Dynamic Text in Android ListView?

DDD
Release: 2024-11-02 00:41:31
Original
401 people have browsed it

How to Create a Custom Row Layout with a Static Header and Dynamic Text in Android ListView?

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>
Copy after login

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>
Copy after login
<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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!