Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

android - Dynamically add items in list view

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 - Dynamically add elements to a listView Android

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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();

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-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

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.

Thanks
sHaH


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...