Dynamically add items in list view

前端 未结 3 1824
南旧
南旧 2020-12-15 02:04

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 sen

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 02:40

    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 listItems = new ArrayList();
    

    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

      
    

    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

提交回复
热议问题