Home > Java > javaTutorial > body text

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

Linda Hamilton
Release: 2024-10-28 18:36:29
Original
237 people have browsed it

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

Android Custom Row Item for ListView

Overview

This article will showcase how to create custom row items for a ListView in Android, allowing you to display data in specific formats.

Challenge

The aim is to create a ListView where each row follows a particular layout:

HEADER
Text
Copy after login
Copy after login

The HEADER should remain static while the Text will change periodically.

Solution

1. Custom Layout XML

Add the following row.xml to your layout folder:

<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

2. Main XML Layout

Update your main XML layout to include the ListView:

<code class="xml"><?xml version="1.0" encoding="utf-8"?>
<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

3. Custom Adapter

Create a custom adapter class that extends BaseAdapter:

<code class="java">class yourAdapter extends BaseAdapter {

    // ... implementation details ...
}</code>
Copy after login

4. Java Activity

In your main Java activity, set up the ListView and adapter:

<code class="java">public class StackActivity extends Activity {

    ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ... implementation details ...
        listview.setAdapter(new yourAdapter(this, new String[] { "data1", "data2" }));
    }
}</code>
Copy after login

Preview

The result will be a ListView with custom row items displaying the desired layout:

HEADER
Text
Copy after login
Copy after login

The above is the detailed content of How to Create Custom Row Items for a ListView in Android with a Static Header and Dynamic Text?. 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
Latest Articles by Author
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!