如何在Android上向ListView动态添加元素? [英] How do you dynamically add elements to a ListView on Android?

查看:294
本文介绍了如何在Android上向ListView动态添加元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释或建议一个教程来动态创建 ListView 在Android中?

Can anyone explain or suggest a tutorial to dynamically create a ListView in android?

这是我的要求:

  • 我应该能够通过按下按钮来动态添加新元素.
  • 应该足够简单易懂(可能未进行任何性能改进或 convertView ,例如)
  • I should be able to dynamically add new elements by pressing a button.
  • Should be simple enough to understand (possibly without any performance improvements or convertView, for instance)

我知道有关此主题的问题很多,但找不到能回答我问题的问题.

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,该ID定义了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<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆