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:
<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>
Main XML Layout:
<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>
Custom Adapter Class:
<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>
Java Activity:
<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>
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!