添加列表视图项时出错

时间:2018-07-13 10:20:04

标签: android listview arraylist

Listview项目停止自动更新列表中的内容。添加列表项时,我不必使用notifydatasetchanged(),并且无需刷新屏幕即可更新它。但是,在添加对我的代码的数据库引用之后,列表视图仅在锁定和解锁手机时更新。我尝试注释掉databsereference代码,但它似乎仍然不起作用。有什么建议吗?

更新:我已经附上了下面的代码。使用notifydatasetchanged()后,列表会自动更新,但是所有listview项都更改为最近添加的项。

    ListView=findViewById(R.id.swipemenu);
      HashMap<String,String> item = new HashMap<String,String>();
    ArrayList<HashMap<String,String>> list=new 
                    ArrayList<HashMap<String,String>>();
   SimpleAdapter adapter=new SimpleAdapter(this, list,
            R.layout.simplelist,
            new String[] { "line1","line2" },
            new int[] {R.id.text1, R.id.text2});
    ListView.setAdapter(adapter);

  item.put( "line1",result.getContents() );
                item.put( "line2","Qty:1" );
                list.add( item );
                adapter.notifyDataSetChanged();



   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        if (result.getContents() == null) {
            Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
        } else {
            try {
                JSONObject obj = new JSONObject(result.getContents());

            } catch (JSONException e) {
                e.printStackTrace();

                item.put( "line1",result.getContents() );
                item.put( "line2","Qty:1" );
                list.add( item );
                adapter.notifyDataSetChanged();


            }
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

0 个答案:

没有答案