Home > Java > javaTutorial > body text

How to Create a ListView with Custom Row Items and Dynamically Changing Text in Android?

Linda Hamilton
Release: 2024-10-29 10:58:29
Original
461 people have browsed it

How to Create a ListView with Custom Row Items and Dynamically Changing Text in Android?

Customize ListView Row Item in Android

The task at hand involves creating a ListView with rows displaying a header followed by changing text. To achieve this, follow the steps outlined below:

Custom Layout for Row Item:

  • Create a custom row layout named 'row.xml' in 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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"/>
</LinearLayout></code>
Copy after login

Main XML Layout:

  • Update your main XML layout to include a 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">
    </ListView>
</LinearLayout></code>
Copy after login

Custom Adapter Class:

  • Create a custom adapter class that extends BaseAdapter:
<code class="java">class yourAdapter extends BaseAdapter {

    Context context;
    String[] data;
    private static LayoutInflater inflater = null;

    public yourAdapter(Context context, String[] data) {
        this.context = context;
        this.data = data;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    // ... Implement other methods as required by BaseAdapter
    // such as getView(), getCount(), getItem(), getItemId()

}</code>
Copy after login

Java Activity:

  • In your Java activity, set up the ListView and adapter:
<code class="java">public class StackActivity extends Activity {

    ListView listview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listview = (ListView) findViewById(R.id.listview);
        listview.setAdapter(new yourAdapter(this, new String[] { "data1",
                "data2" }));
    }
}</code>
Copy after login

This approach will result in a ListView with custom row items displaying the "Header" text above dynamic text that updates periodically.

The above is the detailed content of How to Create a ListView with Custom Row Items and Dynamically Changing Text in Android?. 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