如何在Android中动态地将元素添加到listView [英] How to add elements to a listView dynamically in Android
问题描述
有人可以解释或建议在Android中创建listView的教程吗?
Can anyone explain or suggest a tutorial to create a listView in android?
这是我的要求:
- 我应该能够通过按下按钮来动态添加新元素.
- 应该足够简单易懂(例如,可能没有任何性能改进或convertview)
我知道有关此主题的问题很多,发布在此处的StackOverflow上,但是找不到任何答案可以回答我的问题.谢谢!
I know there are quite a few questions on this topic, posted here on StackOverflow, but couldn't find any that would answer my question. Thanks!
推荐答案
首先在项目的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
,该ID定义了ListView
可以使用的默认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<String> adapter
处理,应使用以下方式通知:
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屋!