你如何在 Android 上动态地向 ListView 添加元素? [英] How do you dynamically add elements to a ListView on Android?
问题描述
谁能解释或建议一个教程来动态创建ListView
在安卓中?
Can anyone explain or suggest a tutorial to dynamically create a ListView
in android?
这是我的要求:
- 我应该能够通过按下按钮来动态添加新元素.
- 应该足够简单易懂(可能没有任何性能改进或convertView,例如)
我知道关于这个主题有很多问题,但我找不到能回答我问题的任何问题.
I know there are quite a few questions on this topic, but I couldn't find any that answer my question.
推荐答案
首先在项目的 res/layout/main.xml
文件夹中创建一个 XML 布局:
Create an XML layout first in your project's res/layout/main.xml
folder:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/addBtn"
android:text="Add New Item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
这是一个简单的布局,顶部有一个按钮,底部有一个列表视图.请注意,ListView
的 id @android:id/list
定义了 ListActivity
可以使用的默认 ListView
.
This is a simple layout with a button on the top and a list view on the bottom. Note that the ListView
has the id @android:id/list
which defines the default ListView
a ListActivity
can use.
public class ListViewDemo extends ListActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems=new ArrayList<String>();
//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;
//RECORDING HOW MANY TIMES THE BUTTON HAS BEEN CLICKED
int clickCounter=0;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
adapter.notifyDataSetChanged();
}
}
android.R.layout.simple_list_item_1
是 Android 提供的默认列表项布局,您可以将此库存布局用于非复杂的事情.
android.R.layout.simple_list_item_1
is the default list item layout supplied by Android, and you can use this stock layout for non-complex things.
listItems
是一个 List,它保存了 ListView 中显示的数据.所有的插入和删除都应该在 listItems
上完成;listItems
中的更改应该反映在视图中.这由 ArrayAdapter
,应使用以下方式通知:
listItems
is a List which holds the data shown in the ListView. All the insertion and removal should be done on listItems
; the changes in listItems
should be reflected in the view. That's handled by ArrayAdapter<String> adapter
, which should be notified using:
adapter.notifyDataSetChanged();
使用 3 个参数实例化适配器:上下文,可以是您的 activity/listactivity
;您的个人列表项的布局;最后是列表,即列表中要显示的实际数据.
An Adapter is instantiated with 3 parameters: the context, which could be your activity/listactivity
; the layout of your individual list item; and lastly, the list, which is the actual data to be displayed in the list.
这篇关于你如何在 Android 上动态地向 ListView 添加元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!