动态添加元素到ListView的Andr​​oid [英] Dynamically add elements to a listView Android

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

问题描述

谁能解释或提出了一个教程,创建Android的列表视图?

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

下面是我的要求:

  • 我应该能够动态地添加由pressing一个按钮,新的元素。
  • 应该是非常简单理解(有可能没有任何业绩改善或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!

推荐答案

首先创建一个XML布局在项目的 RES /布局/ main.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 /列表定义默认的ListView A ListActivity 可以使用。

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&LT;字符串&GT;适配器,应使用通知:

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个参数:上下文,这可能是你的活动/ 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.

这篇关于动态添加元素到ListView的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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