如何使用列表<;字符串>;对于ArrayAdapter值(Java、Android)

如何使用列表<;字符串>;对于ArrayAdapter值(Java、Android),android,listview,items,Android,Listview,Items,我试图在应用程序运行时更新listView项。我可以通过使用字符串[]而不是列表使其工作,但我的目标是使用列表使其工作,以便在应用程序运行时向listView添加更多项 private List<String> items; 我希望在用户使用“adapter.notifyDataSetChanged();”将项目添加到列表时实时管理这些项目 我是Java初学者,尤其是Android开发方面的初学者,因此非常感谢您的帮助。您有两种方法可以做到这一点: 将列表转换为数组(您可以找到一

我试图在应用程序运行时更新listView项。我可以通过使用字符串[]而不是列表使其工作,但我的目标是使用列表使其工作,以便在应用程序运行时向listView添加更多项

private List<String> items;
我希望在用户使用“adapter.notifyDataSetChanged();”将项目添加到列表时实时管理这些项目


我是Java初学者,尤其是Android开发方面的初学者,因此非常感谢您的帮助。

您有两种方法可以做到这一点:

  • 将列表转换为数组(您可以找到一个很好的示例)
  • 创建自己的适配器和使用列表(另一个很好的例子)
第一种方法

List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");

String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);

for(String s : stockArr)
    System.out.println(s);
public class CustomAdapter extends BaseAdapter{   

    String [] result;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;

    public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        context=mainActivity;
        imageId=prgmImages;
         inflater = ( LayoutInflater )context.
                 getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
}

该死没有通过谷歌找到这样的信息。我想我才刚刚开始,所以我不知道足够好的关键字。“谢谢你,阿米尔。”怪人。如果答案给你足够的信息,你可以接受。当然可以。等待它。站点需要5分钟我才能接受。您可以扩展ArrayAdapter而不是BaseAdapter。否则,您就忽略了需要实现
getCount
和such@cricket_007是的,你说得对。我只是粘贴了一些必要的代码'_(ツ)_/假设您初始化了arraylist,那么您的代码应该可以工作。
adapter.add(“hello”)
将更新列表,而无需
notifyDataSetChanged
List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");

String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);

for(String s : stockArr)
    System.out.println(s);
public class CustomAdapter extends BaseAdapter{   

    String [] result;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;

    public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        context=mainActivity;
        imageId=prgmImages;
         inflater = ( LayoutInflater )context.
                 getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
}