动态添加列表视图中的项目 [英] Dynamically add items in list view

查看:185
本文介绍了动态添加列表视图中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个动态的列表视图,获取用户的凭据,当我登录的第一次,并在列表中显示它的下一次我启动应用程序。我知道如何从一个意图发送用户名到另一个。我还没有集中在SQLite的一部分,然而,稍后会做。我现在面临创建动态列表视图的问题。 发现了一个非常有用的线索 - 动态添加元素到ListView的Andr​​oid

I want to make a dynamic list view which gets the user credentials when I login for the first time and displays it in a list the next time I start the app. I know how to send the username from one intent to another. i haven't focused on the SQLite part yet, will do that later. I'm facing problems in creating the dynamic list view. Found one very useful thread - Add dynamically elements to a listView Android

他用屏幕上的一个按钮,并调用的方法的onClick来填充列表。我可以不用按钮?我希望它会自动发生一次,我能够登录。 我如何使用的语句在我的code?

he used a button on the screen and called the method onClick to populate the list. Can i do it without the button? I want it to automatically happen once i am able to login. how can i use the statements in my code?

listItems.add(value);
adapter.notifyDataSetChanged();

下面的值是用户名,我从一些其他的意图得到。
请帮忙。谢谢!

here value is the username i am getting from some other intent.
please help. thanks!

推荐答案

有关下面给出这只需使用例如: 比如你要添加一些字符串到你的列表

For this Just use the example given below: For Instance you are Adding Some Strings into your List

那么创建一个这样的ListArray

So Create a ListArray like this

ArrayList<String> listItems = new ArrayList<String>();

现在,每当你想添加特定字符串到列表中只是做这件事情

now whenever you want to add certain string into list just do this thing

  EditText editText = (EditText) findViewById(R.id.edit);
  listItems.add("my string");  OR
  listItems.add(editText.getText.toString()); //incase if you are getting string value from editText and adding it into the list

使用这个XML的线性布局中main.xml中

Use this Xml inside your linear layout in main.xml

  <EditText android:id="@+id/edit"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

当你添加一个项目动态再调用这个现在

Now when you have added one item dynamically then call this

  adapter.notifyDataSetChanged();

以上将更新你的清单,显示upadted列表。

The above will update your list and display the upadted list.

有关此的详细信息请参阅以下链接:

For more info about this see the following links:

http://www.androidpeople.com/android-custom-列表视图 - 教程部分-1
http://www.androidpeople.com/android-custom-listview-tutorial - 第2
http://www.androidpeople.com/android-custom-动态listview-%E2%80%93part3

http://www.androidpeople.com/android-custom-listview-tutorial-part-1
http://www.androidpeople.com/android-custom-listview-tutorial-part-2
http://www.androidpeople.com/android-custom-dynamic-listview-%E2%80%93part3

在这些教程中,你可以使用ArrayList的替换字符串[]作为在回答OOK顶部给出,当你想添加任何项目只是简单地使用第二code段。

In these tutorials you can replace String[] with ArrayList as given at the top of the answer ook and when you want to add any item just simply use the second code snippet.

感谢
沙阿

这篇关于动态添加列表视图中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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