use android dynamicaly load more items to the listview need help

后端 未结 2 1891
深忆病人
深忆病人 2021-02-06 19:33
package fshizzle.com;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

imp         


        
2条回答
  •  暖寄归人
    2021-02-06 20:04

    A list view can dynamically load items using an Adapter. The development documents have a great tutorial on how to use a ListView and GridView. Basically you set the adapter of your ListView with a collection of objects. Then when you update the objects, you can call notifyDataSetChanged.

    ArrayList myitems = new ArrayList();
    myitems.add("Hello");
    myitems.add("Line 2");
    myitems.add("Another line");
    
    ListView listView = (ListView)findViewById(R.id.mylistview);
    ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_item, myitems));
    listView.setAdapter(adapter);
    
    myitems.add("Goodbye");
    adapter.notifyDataSetChanged();
    

    R.id.mylistview is the id I gave my ListView

    R.layout.list_item is the layout for each list item. It's just a simple TextView

提交回复
热议问题