Android中List与Map<String,Object>及SimpleAdapter用来存储数据并展示出来的联合用法

List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
  String[] s=efoodname.split(" ");
  String[] s01=efoodinfo.split(" ");
  for(int i=0;i<s.length;i++){
   Map<String,Object> map=new HashMap<String,Object>();
   map.put("TextView04", s[i]);
   map.put("TextView05", s01[i]);
   data.add(map);   
  }
  SimpleAdapter sa=new SimpleAdapter(this,data,R.layout.ex_foodinfo,new String[]{"TextView04","TextView05"},new int[]{R.id.TextView04,R.id.TextView05});
  setListAdapter(sa);

 

 

关于Map<String,Object>的用法与作用:

 

Object java.util.Map.put(String key, Object value)

public abstract V put (K key, V value)
Since: API Level 1

Maps the specified key to the specified value.(为被指定的键映射被指定的值,也就是说创建一个映射,即让一个指定的键代表一个指定的值)

Parameters
key the key.
value the value.
Returns
  • the value of any previous mapping with the specified key or null if there was no mapping.(任何一个值映射到一个指定的键或者NULL(如果没有映射关系的话返回null))

 

 

 

 

下面这两行代码的作用解释如下:

 

  String[] s=efoodname.split(" ");
  String[] s01=efoodinfo.split(" ");

 

可以用它们将一个字符串中用空格符分隔开的内容分离成一个新的数组。下面举一个例子来说明

public class Test01 {
   public static void main(String args[])    {
       String s="My name is Ada";
       String[] s01=s.split(" ");
       for(int i=0;i<s01.length;i++){
           System.out.println(s01[i]);
       }
   }
}

 

运行上面的那个程序会出现这样的结果:

run:
My
name
is
Ada
成功生成(总时间:0 秒)

说明String[] s01=s.split(" ");这句话的作用是把字符串s中用空格符来分隔开的内容分离成一个一个的元素并组成一个新的字符类型的数组s01,即s01={"My","name","is","Ada"}.

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值