Android 将项目添加到listview的按钮

Android 将项目添加到listview的按钮,android,listview,Android,Listview,我知道这个问题到处都是,但是即使我浏览了所有的帖子并试图理解,我也无法解决这个问题。我对用Android开发应用非常陌生,所以我肯定需要很多帮助。总之,我在网上找到了一些代码,基本上按照我希望的布局方式创建了一个listview,您可以向listview添加项目。这是密码- ListView lv; ArrayList<HashMap<String,String>> feedList; @Override protected void onCreate(Bundle s

我知道这个问题到处都是,但是即使我浏览了所有的帖子并试图理解,我也无法解决这个问题。我对用Android开发应用非常陌生,所以我肯定需要很多帮助。总之,我在网上找到了一些代码,基本上按照我希望的布局方式创建了一个listview,您可以向listview添加项目。这是密码-

 ListView lv;
ArrayList<HashMap<String,String>> feedList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_check_out);

    lv = (ListView) findViewById(R.id.CheckList);

    feedList= new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("item", "Item");
    map.put("qty", "Qty");
    map.put("tax", "Tax");
    map.put("total", "Total");
    feedList.add(map);

    SimpleAdapter simpleAdapter = new SimpleAdapter(this, feedList, R.layout.list_view_customs, new String[]{"item", "qty", "tax", "total"}, new int[]{R.id.textViewItem, R.id.textViewQty, R.id.textViewTax, R.id.textViewTotal});
    lv.setAdapter(simpleAdapter);
正如您所看到的,通过hashmaps的实现,项目被添加到arraylist中,然后适配器接收数据arraylist以创建列表视图,该列表视图由setAdapter功能设置

因此,现在由于我想通过单击按钮填充listview,我认为这可能会起作用:

ListView lv;
Button addBanana;
ArrayList<HashMap<String,String>> feedList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_check_out);

    lv = (ListView) findViewById(R.id.CheckList);
    addBanana = (Button) findViewById(R.id.AddItem);

    feedList= new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("item", "Item");
    map.put("qty", "Qty");
    map.put("tax", "Tax");
    map.put("total", "Total");
    feedList.add(map);

    SimpleAdapter simpleAdapter = new SimpleAdapter(this, feedList, R.layout.list_view_customs, new String[]{"item", "qty", "tax", "total"}, new int[]{R.id.textViewItem, R.id.textViewQty, R.id.textViewTax, R.id.textViewTotal});
    lv.setAdapter(simpleAdapter);

        addBanana.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         map = new HashMap<String, String>();
         map.put("item", "Banana");
         map.put("qty", "1");
         map.put("tax", "0.13");
         map.put("total", "1.13");
         feedList.add(map);
         simpleAdapter.notifyDataSetChanged();
        }
    });
}

它能完美地建立起来。但是,当我试图通过单击主活动中的按钮进入此活动时,屏幕变暗,并显示退出。。因此,我不确定这种方法有什么错。我知道我处理适配器的方式肯定有问题。。。所以,任何反馈都很好。。非常感谢


另外,如果您需要XML,请告诉我

试试这个,看看它是否能工作,而不是simpleAdapter.notifyDataSetChanged;用新的对象数据集再次初始化它,看看是否会有相同的事件序列,我似乎从来没有使用SimpleAdapter,因为我从来没有机会在运行时使用这个SimpleAdapter.addobject o添加内容,因为它从未存在过。。那是我开始的时候,再也没用过。是的,没有变化。。。