Android ListView动态添加数据

时间:2011-07-17 17:05:00

标签: android listview dynamic

我有一个listView,我想动态添加数据。 这是我想要将数据添加到的XML。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView android:id="@+id/rowTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="70dp"
    android:padding="12dp"
    android:textSize="16sp" >
  </TextView>

  <TextView android:id="@+id/alarm_name_text" 
   android:layout_height="wrap_content" 
   android:text="" 
   android:layout_below="@+id/rowTextView" 
   android:layout_width="fill_parent">
   </TextView>

  <CheckBox android:id="@+id/CheckBox01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp"
    android:layout_alignParentRight="true" android:layout_marginRight="6sp"
    android:focusable="false">
  </CheckBox>

</RelativeLayout>

我想在用户在editText dialogView中输入一些数据后,使用id alarm_name_text将数据添加到第二个TextView。我被告知要创建一个函数,将数据添加到我的ArrayList /适配器中,但我不确定何时调用它或它是如何被使用的......我需要帮助plz :(。这是函数。

public void addItems(View v)
        {
            rowSavedText.setText(getString());
            planetList.add(new Planet("This one"));
            listAdapter.notifyDataSetChanged();
        }

2 个答案:

答案 0 :(得分:1)

您需要处理呈现各行的代码部分。请查看http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx上的教程:具体说明如何使用getView

答案 1 :(得分:0)

只需将元素添加到ArrayList并将notifyDataSetChanged()调用到您的数组适配器。