【Android】ListView修改item布局与内部数据

来源:互联网 发布:深海里的星星知乎 编辑:程序博客网 时间:2024/05/03 16:48

ListView的自定义item样式使用一个adapter来进行适配,直接上代码。

 list = new ArrayList<>();        for (NoteContent noteContent:noteContents){            HashMap<String, String> map = new HashMap<String, String>();            map.put("date",noteContent.date+"  ");            if (noteContent.detail.length()>10)            map.put("detail",noteContent.detail.substring(0,10));            else map.put("detail",noteContent.detail);            map.put("recordurl",noteContent.record_url);            map.put("picurl",noteContent.pic_url);            map.put("barcodeurl",noteContent.barcode_url);            map.put("dbnumber",String.valueOf(noteContent.id));            list.add(map);        }        SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.list_items_layout,                new String[]{"detail","date","dbnumber"},new int[]{R.id.list_items_detail,R.id.list_items_date,R.id.list_items_dbnumber});        listView.setAdapter(adapter);

这里list是一个Map型的ArrayList
private ArrayList<Map<String, String>> list;
用来存放数据,这些数据会在listView中的item一一显示,由于一一对应关系,在ListView中的位置position即可对应list中的下标。

注:
修改数据时,直接修改list中内容,然后重新将list显示,而不要去修改item里面控件的内容。

若要修改item中的控件,通过返回布局来进行id的查找,核心代码如下:

 RelativeLayout layout=(RelativeLayout)listView.getChildAt(position);                Button bt_delete=(Button)layout.findViewById(R.id.list_items_bt_delete);                if (bt_delete.getVisibility()==View.VISIBLE)                             bt_delete.setVisibility(View.GONE);                     else    bt_delete.setVisibility(View.VISIBLE);

这里注意与自身xml中的布局对应,获取了item布局之后再根据R.id进行寻找即可。

0 0
原创粉丝点击